best practicesIntermediate10 min

Robust Error Handling

Build resilient API integrations

Implement comprehensive error handling for Olympus APIs, including retry logic, fallback strategies, and graceful degradation patterns.

01

Error Response Format

All Olympus APIs return consistent error responses with error codes, messages, and optional details for debugging.

json
{
  class="text-emerald-class="text-amber-400">400">"error": {
    class="text-emerald-class="text-amber-400">400">"code": class="text-emerald-class="text-amber-400">400">"RATE_LIMIT_EXCEEDED",
    class="text-emerald-class="text-amber-400">400">"message": class="text-emerald-class="text-amber-400">400">"Too many requests",
    class="text-emerald-class="text-amber-400">400">"details": {
      class="text-emerald-class="text-amber-400">400">"retryAfter": class="text-amber-400">60,
      class="text-emerald-class="text-amber-400">400">"limit": class="text-amber-400">1000,
      class="text-emerald-class="text-amber-400">400">"remaining": class="text-amber-400">0
    }
  }
}
02

Common Error Codes

Understand common error codes: 400 (Bad Request), 401 (Unauthorized), 403 (Forbidden), 404 (Not Found), 429 (Rate Limited), 500 (Server Error).

03

Retry Strategies

Implement different retry strategies based on error type. Retry 429 and 5xx errors; do not retry 4xx client errors.

typescript
class="text-pink-400">function shouldRetry(status: number): boolean {
  class="text-pink-400">return status === class="text-amber-400">429 || status >= class="text-amber-400">500;
}

class="text-pink-400">async class="text-pink-400">function resilientFetch(url: string, options = {}) {
  class="text-pink-400">const maxRetries = class="text-amber-400">3;

  class="text-pink-400">for (class="text-pink-400">let attempt = class="text-amber-400">0; attempt < maxRetries; attempt++) {
    class="text-pink-400">try {
      class="text-pink-400">const response = class="text-pink-400">await fetch(url, options);

      class="text-pink-400">if (response.ok) class="text-pink-400">return response;
      class="text-pink-400">if (!shouldRetry(response.status)) class="text-pink-400">throw class="text-pink-400">new Error();

      class="text-pink-400">await sleep(Math.pow(class="text-amber-400">2, attempt) * class="text-amber-400">1000);
    } class="text-pink-400">catch (error) {
      class="text-pink-400">if (attempt === maxRetries - class="text-amber-400">1) class="text-pink-400">throw error;
    }
  }
}
04

Graceful Degradation

Design your application to degrade gracefully when API calls fail. Cache previous responses and provide meaningful fallbacks.

Related Guides

Ready to implement?

Explore our API documentation to start building with Olympus.