APIError instances with structured metadata for error handling.
Import
import { APIError } from "@paxoslabs/amplify-sdk";
APIError Class
class APIError extends Error {
/** Error code for programmatic handling */
code: string;
/** SDK function that threw the error */
endpoint: string;
/** HTTP status code (if applicable) */
statusCode?: number;
/** Original error message */
message: string;
}
Usage
import { APIError, prepareDepositAuthorization } from "@paxoslabs/amplify-sdk";
try {
const auth = await prepareDepositAuthorization(params);
} catch (error) {
if (error instanceof APIError) {
console.log(error.code); // "VAULT_NOT_FOUND"
console.log(error.endpoint); // "prepareDepositAuthorization"
console.log(error.statusCode); // 404
console.log(error.message); // "No vault matches the provided parameters"
}
}
Error Codes
Initialization Errors
| Code | Description | Resolution |
|---|---|---|
SDK_NOT_INITIALIZED | SDK function called before initAmplifySDK() | Call initAmplifySDK() at app startup |
INVALID_API_KEY | API key validation failed | Check key format (pxl_...) |
Deposit Errors
| Code | Description | Resolution |
|---|---|---|
VAULT_NOT_FOUND | No vault matches yieldType, token, and chainId | Verify parameters with fetchSupportedAssets() |
PERMIT_NOT_SUPPORTED | Token doesn’t support EIP-2612 permits | Use approval flow or forceMethod: "approval" |
INSUFFICIENT_ALLOWANCE | Approval amount insufficient for deposit | Increase approval or use permit flow |
INVALID_DEPOSIT_AMOUNT | Amount is zero, negative, or malformed | Provide valid decimal string (e.g., “100.25”) |
Withdrawal Errors
| Code | Description | Resolution |
|---|---|---|
VAULT_NOT_FOUND | No vault matches parameters | Verify yieldType and wantAssetAddress |
INSUFFICIENT_SHARES | User doesn’t have enough vault shares | Check share balance before withdrawing |
LIQUIDITY_UNAVAILABLE | Vault has insufficient liquidity | Try smaller amount or wait |
Network Errors
| Code | Description | Resolution |
|---|---|---|
NETWORK_ERROR | Failed to connect to RPC or API | Check network connection and RPC URL |
TIMEOUT | Request timed out | Retry with backoff |
Error Handling Patterns
Basic Error Handling
try {
const auth = await prepareDepositAuthorization(params);
} catch (error) {
if (error instanceof APIError) {
switch (error.code) {
case "SDK_NOT_INITIALIZED":
await initAmplifySDK(apiKey);
// Retry operation
break;
case "VAULT_NOT_FOUND":
showError("This token is not supported for the selected yield type");
break;
default:
showError(error.message);
}
}
}
Logging Errors
try {
const prepared = await prepareDeposit(params);
} catch (error) {
if (error instanceof APIError) {
// Log structured error data
console.error({
code: error.code,
endpoint: error.endpoint,
statusCode: error.statusCode,
message: error.message,
});
// Send to error tracking service
errorTracker.capture(error, {
extra: {
endpoint: error.endpoint,
statusCode: error.statusCode,
},
});
}
}
User-Friendly Messages
function getErrorMessage(error: APIError): string {
const messages: Record<string, string> = {
SDK_NOT_INITIALIZED: "Please wait while we initialize...",
VAULT_NOT_FOUND: "This token is not available for the selected strategy.",
PERMIT_NOT_SUPPORTED: "Please approve the transaction in your wallet.",
INSUFFICIENT_ALLOWANCE: "Please approve a higher amount.",
LIQUIDITY_UNAVAILABLE: "Withdrawals are temporarily limited. Please try a smaller amount.",
};
return messages[error.code] || error.message;
}
Related
- Troubleshooting - Common issues and solutions
- Logging Guide - Debug logging configuration