⚠️ 8 Minute Guide

Error Handling

Best practices for handling API errors, retry logic, and graceful failure management.

1

Error Response Format

Understand the structure of error responses from our API.

// Typical error response structure
{
  "error": {
    "code": "insufficient_funds",
    "message": "Insufficient funds in the account",
    "details": {
      "available_balance": 150.00,
      "requested_amount": 200.00
    },
    "request_id": "req_1234567890"
  }
}
2

Implement Retry Logic

Handle temporary failures with exponential backoff.

async function retryRequest(apiCall, maxRetries = 3) {
  for (let attempt = 0; attempt <= maxRetries; attempt++) {
    try {
      const response = await apiCall();
      return response;
    } catch (error) {
      if (attempt === maxRetries || !isRetryableError(error)) {
        throw error;
      }
      
      // Exponential backoff
      const delay = Math.pow(2, attempt) * 1000;
      await new Promise(resolve => setTimeout(resolve, delay));
    }
  }
}
3

Error Classification

Categorize errors and handle them appropriately.

function handleApiError(error) {
  const errorCode = error.error?.code;
  
  switch (errorCode) {
    case 'insufficient_funds':
      showUserMessage('Insufficient funds. Please add money to continue.');
      break;
    
    case 'invalid_card':
      showUserMessage('Invalid card details. Please check and try again.');
      break;
    
    case 'rate_limit_exceeded':
      showUserMessage('Too many requests. Please wait a moment.');
      break;
    
    default:
      showUserMessage('Something went wrong. Please try again.');
  }
}

⚠️ Error Handling Best Practices

🔄 Retry Strategy

Use exponential backoff for temporary failures

👤 User Experience

Show meaningful error messages to users

📝 Logging

Log errors with request IDs for debugging

🔍 Monitoring

Set up alerts for critical error rates