2023-07-20 09:56:28 +02:00
|
|
|
import pkg/metrics
|
2022-11-08 02:10:17 -05:00
|
|
|
import ../statemachine
|
2024-10-10 13:53:33 +02:00
|
|
|
import ../../logutils
|
2025-02-19 11:18:45 +11:00
|
|
|
import ../../utils/exceptions
|
2022-11-08 02:10:17 -05:00
|
|
|
import ./error
|
|
|
|
|
|
2023-11-03 17:21:54 +02:00
|
|
|
declareCounter(codex_purchases_failed, "codex purchases failed")
|
2023-07-20 09:56:28 +02:00
|
|
|
|
2022-11-08 02:10:17 -05:00
|
|
|
type PurchaseFailed* = ref object of PurchaseState
|
|
|
|
|
|
2023-06-05 10:48:06 +02:00
|
|
|
method `$`*(state: PurchaseFailed): string =
|
2022-11-08 02:10:17 -05:00
|
|
|
"failed"
|
2023-06-05 10:48:06 +02:00
|
|
|
|
2025-02-19 11:18:45 +11:00
|
|
|
method run*(
|
|
|
|
|
state: PurchaseFailed, machine: Machine
|
|
|
|
|
): Future[?State] {.async: (raises: []).} =
|
2023-11-03 17:21:54 +02:00
|
|
|
codex_purchases_failed.inc()
|
2024-10-10 13:53:33 +02:00
|
|
|
let purchase = Purchase(machine)
|
2025-02-19 11:18:45 +11:00
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
warn "Request failed, withdrawing remaining funds", requestId = purchase.requestId
|
|
|
|
|
await purchase.market.withdrawFunds(purchase.requestId)
|
|
|
|
|
except CancelledError as e:
|
|
|
|
|
trace "PurchaseFailed.run was cancelled", error = e.msgDetail
|
|
|
|
|
except CatchableError as e:
|
|
|
|
|
error "Error during PurchaseFailed.run", error = e.msgDetail
|
|
|
|
|
return some State(PurchaseErrored(error: e))
|
2024-10-10 13:53:33 +02:00
|
|
|
|
2023-06-05 10:48:06 +02:00
|
|
|
let error = newException(PurchaseError, "Purchase failed")
|
|
|
|
|
return some State(PurchaseErrored(error: error))
|