Error Handling

Handle errors in the TypeScript SDK

Handle errors gracefully in your application.

Error Types

import { Client, AuthenticationError, APIError, NetworkError } from '@limitry/sdk';

try {
  const client = new Client({ apiKey: 'invalid-key' });
  await client.project.get();
} catch (error) {
  if (error instanceof AuthenticationError) {
    console.error('Authentication failed:', error);
    console.error('Status code:', error.status);
  } else if (error instanceof APIError) {
    console.error('API error:', error);
    console.error('Status:', error.status, error.reason);
  } else if (error instanceof NetworkError) {
    console.error('Network error:', error);
  }
}

Retry Logic

async function trackWithRetry(client: Client, maxRetries = 3) {
  for (let attempt = 0; attempt < maxRetries; attempt++) {
    try {
      await client.track.trackUsage(/docs/* ... *);
      return;
    } catch (error) {
      if (error instanceof APIError && error.status === 429) {
        // Rate limited, exponential backoff
        await new Promise(resolve => setTimeout(resolve, 2 ** attempt * 1000));
      } else {
        throw error;
      }
    }
  }
}

Next Steps

On this page