Fanbeam
Fanbeam
Documentation
API Reference
Getting Started
User Guides
TypeScript SDK
AuthenticationError HandlingInstallationQuick StartTypeScript Types
TypeScript SDK

Error Handling

Handle errors in the SDK

The Fanbeam SDK provides consistent error handling across all methods.

Response Structure

All SDK methods return a response object:

type Response<T> = {
  data?: T;
  error?: {
    status: number;
    body: unknown;
  };
};

Checking for Errors

Always check for errors before accessing data:

import { posts } from 'fanbeam';

const response = await posts.list();

if (response.error) {
  console.error('Error:', response.error.status, response.error.body);
  return;
}

// Safe to access data
console.log('Posts:', response.data);

Common Error Types

Authentication Errors

if (response.error?.status === 401) {
  console.error('Authentication failed. Check your API key.');
}

Rate Limit Errors

if (response.error?.status === 429) {
  console.error('Rate limit exceeded. Please retry later.');
  const retryAfter = response.error.body?.retryAfter;
  if (retryAfter) {
    await new Promise(resolve => setTimeout(resolve, retryAfter * 1000));
  }
}

Validation Errors

if (response.error?.status === 400) {
  console.error('Validation error:', response.error.body);
}

Not Found Errors

if (response.error?.status === 404) {
  console.error('Resource not found');
}

Error Handling Helper

import { posts } from 'fanbeam';

async function safeListPosts() {
  const response = await posts.list();
  
  if (response.error) {
    switch (response.error.status) {
      case 401:
        throw new Error('Invalid API key');
      case 429:
        throw new Error('Rate limit exceeded');
      case 500:
        throw new Error('Server error');
      default:
        throw new Error(`API error: ${response.error.status}`);
    }
  }
  
  return response.data;
}

Try-Catch Pattern

You can also use try-catch for request-level errors:

import { posts } from 'fanbeam';

try {
  const response = await posts.list();
  if (response.error) {
    // Handle API error
    console.error('API Error:', response.error);
    return;
  }
  console.log('Posts:', response.data);
} catch (error) {
  // Handle network or other errors
  console.error('Request failed:', error);
}

Retry Logic

Implement retry logic for transient errors:

import { posts } from 'fanbeam';

async function listPostsWithRetry(maxRetries = 3) {
  for (let i = 0; i < maxRetries; i++) {
    const response = await posts.list();
    
    if (response.error?.status === 429) {
      const retryAfter = response.error.body?.retryAfter || 60;
      await new Promise(resolve => setTimeout(resolve, retryAfter * 1000));
      continue;
    }
    
    if (response.error?.status === 500 && i < maxRetries - 1) {
      await new Promise(resolve => setTimeout(resolve, 1000 * (i + 1)));
      continue;
    }
    
    return response;
  }
  
  throw new Error('Max retries exceeded');
}

Next Steps

  • Quick Start
  • TypeScript Types

Authentication

Authenticate SDK requests with API keys

Installation

Install the Fanbeam TypeScript SDK

On this page

Response StructureChecking for ErrorsCommon Error TypesAuthentication ErrorsRate Limit ErrorsValidation ErrorsNot Found ErrorsError Handling HelperTry-Catch PatternRetry LogicNext Steps