Skip to content

Error Handling

Error Callbacks

The simplest way to handle errors is through onSwapError:

js
const widget = new TokenFlightWidget({
  container: '#widget',
  config: {
    toToken: { chainId: 8453, address: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913' },
    tradeType: 'EXACT_OUTPUT',
    amount: '100',
    theme: 'dark',
  },
  callbacks: {
    onSwapError: (error) => {
      switch (error.code) {
        case 'TF4003':
          showToast('Insufficient balance');
          break;
        case 'TF2003':
          showToast('Wallet action failed');
          break;
        default:
          showToast(error.message);
      }
    },
  },
});

TokenFlightError

js

try {
  widget.initialize();
} catch (err) {
  if (err instanceof TokenFlightError) {
    console.error(`[${err.code}] ${err.message}`);
    console.error('Details:', err.details);
  }
}

Common Error Codes

CodeNameDescription
TF1001INVALID_CONFIGInvalid configuration provided
TF1002INVALID_TOKEN_IDENTIFIERBad token identifier format
TF1003INVALID_AMOUNTInvalid amount value
TF1004MISSING_REQUIRED_CONFIGMissing required configuration
TF2001WALLET_NOT_CONNECTEDNo wallet connected
TF2003WALLET_ACTION_FAILEDWallet action failed
TF2004WALLET_ACTION_REJECTEDUser rejected a wallet prompt
TF3001API_REQUEST_FAILEDAPI request failed
TF3002API_TIMEOUTAPI request timed out
TF3004QUOTE_FAILEDNo route could be quoted
TF3005QUOTE_EXPIREDQuote expired and needs refresh
TF4001TRANSACTION_FAILEDOn-chain transaction reverted
TF4002TRANSACTION_REJECTEDUser rejected the transaction
TF4003INSUFFICIENT_BALANCENot enough tokens for the route
TF5001ELEMENT_NOT_FOUNDContainer selector did not match the DOM
TF5002INITIALIZATION_FAILEDWidget failed to mount

Scenario Notes

  • Quote-unavailable states may surface as UI state without a dedicated callback.
  • Wallet rejection commonly appears as TF2003 or TF2004, depending on the adapter.
  • Cross-chain recipient validation can block execution before any transaction is sent.