Error Handling Patterns

Build safe, predictable fallback paths when trades don’t go as expected.

Philosophy

Trade execution can fail — due to market changes, balance issues, or API constraints. The goal is not to avoid all errors, but to handle them gracefully, log them clearly, and fail in ways that protect both your user and your business.

1.

Common Error Types

Error CaseStatus CodeExample MessageWhat It Means
Quote expired400"Quote expired"The quote was valid, but not used within its expiry window
Invalid asset pair400"Invalid asset pair"Only usdt ↔ bitcoin swaps are supported
Invalid or zero amount400"Missing or invalid amount"Amount was not supplied or below threshold
Duplicate reference409"Duplicate reference"A trade already exists for this reference — idempotency caught it
Insufficient balance422"Insufficient balance"Wallet doesn’t have enough funds to complete trade
Execution error500"Trade execution failed"Internal error from liquidity routing or engine timeout
2.

Retry Logic

Idempotency is your safety net. If your system doesn’t receive a response due to timeout or network drop:

Retry the request using the same reference If the original succeeded, Bitnob returns the original trade If it failed previously, a 409 or error will be returned without re-execution

This guarantees no duplicate trades are created even under retry conditions.

3.

Quote Expiry Handling

If you’re using quoteId:

You must execute the trade before the expiresAt timestamp

If the quote is expired:

Return a soft error to the user:

"This price has expired. Tap refresh to get a new quote."

Immediately request a fresh quote from /trades/quote

4.

Monitoring Failures

To surface issues before they impact users:

Track all trade.failed webhook events

Alert on failure spikes (e.g., 5+ failures within 1 min)

Log the reason and context (wallet ID, amount, quote ID, chain, user ID)

Include the reference in all logs so issues can be traced across systems

5.

Recovery Paths

Failure CaseSuggested Action
quote expiredShow new quote immediately, don’t auto-retry without user confirmation
insufficient balanceRetry later or trigger background top-up, then queue trade
duplicate referenceTreat as success if confirmed, log conflict if amounts differ
execution errorRetry with backoff, optionally alert ops for trades over threshold
6.

Best Logging Practices

Log every failed trade attempt with:

reference

quoteId (if used)

input amount, source/destination asset failure code or message

createdAt timestamp user or account context

Use this to power dashboards, postmortems, and support flows.

7.

Protecting Users

In all failure cases:

Do not deduct balances until trade.completed is confirmed

Clearly communicate when a trade failed and why

Never “freeze” funds unless you’ve locked execution in your internal system

Offer retry or refresh options in the UI, not silent fails

Did you find this page useful?