LayerHopDocsLaunch app (opens in a new tab)

Error handling & retries

Branch on stable error codes, know which errors to retry, respect rate limits, and recover from expired quotes and provider outages.

Every LayerHop error response has the same JSON shape with a stable code, so handle errors by branching on code: fix the request for validation errors, re-quote for expired quotes, and retry with backoff for provider, rate-limit and internal errors. Every response also carries an x-request-id header to quote when you report a problem.

What does an error look like?#

JSON
{
  "error": {
    "code": "QUOTE_EXPIRED",
    "message": "Quote expired, request a new one",
    "requestId": "0d5e1f6a-3b2c-4d1e-8f9a-7b6c5d4e3f21",
    "docsUrl": "https://docs.layerhop.com/errors/QUOTE_EXPIRED"
  }
}
Field Use it for
code Branching. Codes are stable; new ones may be added.
message Humans. The wording may change.
requestId Logs and support. Same value as the x-request-id header.
docsUrl The error catalog page with causes and fixes.
details Only on some errors. A quote that fails with NO_ROUTE carries details.errors: why each provider returned nothing (provider, code, message), the same entries a successful quote lists in errors. AMOUNT_TOO_SMALL on a quote can also carry the route's minimum: minAmount (base units of srcToken), minAmountUsd and minAmountApprox.

The SDK throws LayerHopApiError with code, status, message and requestId:

TypeScript
import { LayerHopApiError } from "@layerhop/sdk";

try {
  await lh.buildTx(quoteId);
} catch (e) {
  if (e instanceof LayerHopApiError && (e.code === "QUOTE_EXPIRED" || e.code === "QUOTE_NOT_FOUND")) {
    // re-quote
  } else throw e;
}

Which errors should I retry?#

Code HTTP Retry? How
INVALID_PARAMS, UNSUPPORTED_CHAIN, UNSUPPORTED_TOKEN, AMOUNT_TOO_SMALL, AMOUNT_TOO_LARGE, CALLS_REVERTED, LAUNCH_IMAGE_INVALID 400 No Fix the request.
QUOTE_EXPIRED 410 After a new quote Re-quote and ask the user to confirm the new price.
QUOTE_NOT_FOUND 404 After a new quote The quote is unknown or was evicted. Re-quote.
NO_ROUTE 404 With backoff No provider could serve the pair right now. Also try another amount or token.
ORDER_NOT_FOUND 404 With backoff Right after broadcast, the provider may not have indexed the order yet.
RATE_LIMITED 429 Yes Wait the number of seconds in the retry-after header.
PROVIDER_ERROR, PROVIDER_TIMEOUT, INTERNAL 502, 504, 500 With backoff Exponential backoff with jitter, a few attempts.
UNAUTHORIZED 401 No The x-api-key is invalid or revoked. Check the key in the dashboard.
FORBIDDEN, NOT_FOUND, CONFLICT 403, 404, 409 No Dashboard and order-reporting errors; see the catalog.
GASLESS_PENDING 409 With backoff A gasless order is still being relayed; poll GET /v1/orders/gasless/{quoteId}, don't resubmit.
GASLESS_FAILED 422 After a new quote The relayer rejected the signed gasless order; nothing was sent. Re-quote and sign again.
LIMIT_ORDER_REJECTED 422 After fixing the cause A signed EVM limit order was refused (wrong signer, or CoW Protocol rejected it, e.g. allowance too low); nothing was placed.

How do I write a retry helper?#

TypeScript
import { LayerHopApiError } from "@layerhop/sdk";

const RETRYABLE = new Set(["NO_ROUTE", "PROVIDER_ERROR", "PROVIDER_TIMEOUT", "RATE_LIMITED", "INTERNAL", "ORDER_NOT_FOUND"]);

export async function withRetry<T>(fn: () => Promise<T>, attempts = 4, baseMs = 500): Promise<T> {
  for (let i = 0; ; i++) {
    try {
      return await fn();
    } catch (e) {
      const retryable = e instanceof LayerHopApiError ? RETRYABLE.has(e.code) : e instanceof TypeError; // TypeError: network failure
      if (!retryable || i >= attempts - 1) throw e;
      await new Promise((r) => setTimeout(r, baseMs * 2 ** i + Math.random() * baseMs));
    }
  }
}

const quotes = await withRetry(() => lh.quote(params));

When you call the API directly, read retry-after on a 429 instead of guessing; it is the number of seconds until a request will be accepted again. Every rate-limited response also carries x-ratelimit-limit and x-ratelimit-remaining. See rate limits.

How do I handle expired quotes?#

Quotes live up to 60 seconds, and some providers set a shorter expiresAt. A robust UI:

  1. Re-quotes on a timer (for example every 15–20 seconds) while the user looks at a price.
  2. Builds the transaction only after the user confirms.
  3. On QUOTE_EXPIRED or QUOTE_NOT_FOUND, fetches a fresh quote, shows the new output and asks again. Never sign a worse price silently.
TypeScript
async function buildFresh(params: QuoteParams) {
  let { best } = await lh.quote(params);
  if (best.expiresAt - Date.now() / 1000 < 5) ({ best } = await lh.quote(params));
  return { quote: best, tx: await lh.buildTx(best.quoteId) };
}

Can a successful quote contain errors?#

Yes. A 200 from GET /v1/quote can include an errors array: one entry per provider that could not quote, with provider, code and message. Use it for diagnostics ("deBridge: amount too small"), not to block the user, because the quotes you received are valid. Only when no provider quotes does the request fail with NO_ROUTE, and then the same list is in error.details.errors. When every provider rejected the amount as too small or too large (or it is worth more than $10 billion), the code is AMOUNT_TOO_SMALL or AMOUNT_TOO_LARGE instead.

How do I show a route's minimum amount?#

Many bridges have a minimum (often a few dollars, more on routes with expensive destination gas), and not all of them say so: some answer "no route". When every provider fails and the failures look like a below-minimum amount, LayerHop answers AMOUNT_TOO_SMALL with the minimum in error.details:

JSON
{
  "error": {
    "code": "AMOUNT_TOO_SMALL",
    "message": "Amount is below this route's minimum (about $25); minAmount is the smallest probed amount that quoted",
    "details": { "errors": [ ... ], "minAmount": "25000000", "minAmountUsd": 25, "minAmountApprox": true }
  }
}
  • minAmount is in base units of srcToken; minAmountUsd is there when the token has a price.
  • minAmountApprox: false: a provider reported the minimum. true: LayerHop quoted the same route at $10, $25 or $50 (at most two extra quotes, cached for 30 minutes) and reports the smallest that worked, so the true minimum is at most this.
  • Without a minimum the error stays NO_ROUTE.
TypeScript
try {
  await lh.quote(params);
} catch (e) {
  if (e instanceof LayerHopApiError && e.code === "AMOUNT_TOO_SMALL" && typeof e.details?.minAmount === "string") {
    showMinimum(BigInt(e.details.minAmount), e.details.minAmountUsd as number | undefined); // e.g. a "Use minimum" button
  }
}

The LayerHop swap app and widget show "Minimum for this route is about $X (≈ Y TOKEN)" with a Use minimum button.

A 200 can also carry warnings: the request was served, but not exactly as asked. Today the only code is GAS_ON_ARRIVAL_UNAVAILABLE (see Gas on arrival).

How do I catch failures before the user signs?#

Call POST /v1/simulate with the quoteId. It dry-runs the transaction on the source chain and returns ok: false with a reason instead of an HTTP error, so a revert never reaches the wallet prompt.

What if something fails after the user signed?#

Once a transaction is on chain, the order belongs to the provider's protocol. Errors from the status endpoint mean "could not check right now", not "the swap failed". Keep polling with backoff, show the source transaction in the explorer, and make sure you reported the hash so LayerHop keeps tracking it for up to 24 hours. Reporting the same hash twice is safe; reporting a different hash for the same quote returns CONFLICT.

Last updated