Skip to content
✈️ TokenFlight SDK

Error Handling

The simplest way to handle errors is through callbacks:

const swap = new TokenFlightSwap({
container: '#swap',
config: { theme: 'dark' },
callbacks: {
onSwapError: (error) => {
// error.code — machine-readable ErrorCode (e.g. "TF4003")
// error.message — human-readable description
// error.details — optional extra context
switch (error.code) {
case 'TF4003':
showToast('Insufficient balance');
break;
case 'TF4002':
showToast('Transaction rejected by wallet');
break;
default:
showToast(error.message);
}
},
},
});

When using the SDK programmatically, errors are thrown as TokenFlightError instances:

import { TokenFlightError, ErrorCode } from '@tokenflight/swap';
try {
swap.initialize();
} catch (err) {
if (err instanceof TokenFlightError) {
console.error(`[${err.code}] ${err.message}`);
console.error('Details:', err.details);
}
}
CodeNameDescription
TF1001INVALID_CONFIGInvalid configuration provided
TF1002INVALID_TOKEN_IDENTIFIERBad token identifier format (not CAIP-10, JSON, or object)
TF1003INVALID_AMOUNTInvalid amount value
TF1004MISSING_REQUIRED_CONFIGMissing required configuration field
CodeNameDescription
TF2001WALLET_NOT_CONNECTEDNo wallet connected
TF2002WALLET_CONNECTION_FAILEDCould not connect to wallet
TF2003WALLET_ACTION_FAILEDWallet action (sign/send) failed
TF2004WALLET_ACTION_REJECTEDUser rejected the wallet prompt
TF2005UNSUPPORTED_ACTION_TYPEWallet adapter does not support this action type
CodeNameDescription
TF3001API_REQUEST_FAILEDNetwork request to API failed
TF3002API_TIMEOUTRequest timed out
TF3003API_INVALID_RESPONSEUnexpected response shape
TF3004QUOTE_FAILEDCould not get a quote for this pair
TF3005QUOTE_EXPIREDQuote has expired, needs refresh
TF3006DEPOSIT_BUILD_FAILEDCould not build deposit transaction
TF3007DEPOSIT_SUBMIT_FAILEDDeposit submission failed
TF3008ORDER_FAILEDOrder processing failed after deposit
CodeNameDescription
TF4001TRANSACTION_FAILEDOn-chain transaction reverted
TF4002TRANSACTION_REJECTEDUser rejected the transaction
TF4003INSUFFICIENT_BALANCENot enough tokens for the swap
TF4004SLIPPAGE_EXCEEDEDPrice moved beyond slippage tolerance
CodeNameDescription
TF5001ELEMENT_NOT_FOUNDContainer selector did not match any DOM element
TF5002INITIALIZATION_FAILEDWidget failed to mount

The widget shows this in the UI automatically. Use the callback to add your own behavior:

callbacks: {
onSwapError: (error) => {
if (error.code === 'TF4003') {
analytics.track('insufficient_balance', error.details);
}
},
}

If no route is available for the selected pair, TF3004 fires. The widget shows a message, but you can surface it elsewhere:

callbacks: {
onSwapError: (error) => {
if (error.code === 'TF3004') {
showBanner('This route is not available right now');
}
},
}

TF2004 / TF4002 mean the user clicked “Reject” in their wallet. Usually no action is needed — the widget resets to the quote state.