Skip to content

Errors

The SDK throws errors. It does not return V2-style [error, value] tuples.

import {
  AmountOutOfRangeError,
  DepositGasPolicyError,
  DepositRevertedError,
  DepositSimulationError,
  ExactInputMismatchError,
  HttpError,
  MaintenanceModeError,
  PublicSwapRevertedError,
  PublicSwapSimulationError,
  RefundNotAllowedError,
  RefundRevertedError,
} from '@silentswap/sdk';
 
export function describeSilentSwapError(error: unknown): string {
  if (error instanceof MaintenanceModeError) return error.message;
  if (error instanceof AmountOutOfRangeError) {
    return `${error.code}: ${error.limitAmount ?? 'unknown limit'}`;
  }
  if (error instanceof ExactInputMismatchError) {
    return `Quote debit mismatch: asked ${error.requestedAmount}, got ${error.returnedAmount ?? 'nothing'}`;
  }
  if (error instanceof DepositGasPolicyError) {
    return error.unsupportedReason === 'gas-budget'
      ? `Gas cost ${error.maxGasCostWei} exceeds your budget ${error.gasBudgetWei}`
      : `Route needs ${error.gas} gas โ€” above the supported cap`;
  }
  if (error instanceof DepositSimulationError) return error.message;
  if (error instanceof DepositRevertedError) return `Reverted: ${error.depositTxHash}`;
  if (error instanceof PublicSwapSimulationError) return error.message;
  if (error instanceof PublicSwapRevertedError) return error.message;
  if (error instanceof RefundNotAllowedError) return error.message;
  if (error instanceof RefundRevertedError) return `Reverted: ${error.refundTxHash}`;
  if (error instanceof HttpError) return `HTTP ${error.status}`;
  return error instanceof Error ? error.message : 'Unknown SilentSwap error';
}

A DepositSimulationError means no transaction was sent. DepositRevertedError and RefundRevertedError preserve hashes for explorer links. HttpError.body carries the API error payload.

Nothing was broadcast

DepositGasPolicyError is thrown before any wallet request when the route falls outside deposit gas policy. Read unsupportedReason to tell the two cases apart: 'execution-cap' means a clean estimate exceeded the maximum supported execution probe, and 'gas-budget' means the cost exceeded the gasBudgetWei you passed. Both carry gas, and the budget case also carries maxGasCostWei and gasBudgetWei. Neither leaves a pending transaction โ€” tell the user to pick a different route rather than to wait.

ExactInputMismatchError is thrown when an exact-input quote does not report the debit you asked for. requestedAmount and returnedAmount are both base-unit strings; returnedAmount is undefined when the quote reported no debit at all.

Distinguishing reverts

isRevertError(err) walks an unknown error's cause chain and reports whether it bottoms out in a contract revert, rather than matching node-specific message strings. Use it to separate a genuine on-chain rejection from an RPC or transport failure โ€” the first is worth surfacing to the user, the second is worth retrying.

import { isRevertError } from '@silentswap/sdk';
 
export function isWorthRetrying(error: unknown): boolean {
  return !isRevertError(error);
}

Detect wallet user rejection separately from protocol failures so a cancelled signature is not reported as an API incident.