Skip to content

Error Types

Error classes for handling API errors.

DeIterateError

Base error class for all SDK errors.

typescript
class DeIterateError extends Error {
  public readonly code: string;
  public readonly status: number;
  public readonly requestId?: string;
  public readonly details?: Record<string, unknown>;

  constructor(
    message: string,
    code: string,
    status: number,
    requestId?: string,
    details?: Record<string, unknown>
  );
}

Properties

PropertyTypeDescription
codestringError code
statusnumberHTTP status code
requestIdstringRequest ID for debugging
detailsRecord<string, unknown>Additional error details

ValidationError

Thrown when request validation fails (422).

typescript
class ValidationError extends DeIterateError {
  public readonly fieldErrors: Array<{ field: string; message: string }>;

  constructor(
    message: string,
    fieldErrors: Array<{ field: string; message: string }>,
    requestId?: string
  );
}

Example

typescript
try {
  await client.risks.create({ /* missing required fields */ });
} catch (error) {
  if (error instanceof ValidationError) {
    console.log('Validation errors:');
    for (const { field, message } of error.fieldErrors) {
      console.log(`  ${field}: ${message}`);
    }
  }
}

AuthenticationError

Thrown when authentication fails (401).

typescript
class AuthenticationError extends DeIterateError {
  constructor(message: string, code?: string, requestId?: string);
}

Example

typescript
try {
  await client.risks.list();
} catch (error) {
  if (error instanceof AuthenticationError) {
    console.log('Invalid API key');
  }
}

AuthorizationError

Thrown when authorization fails (403).

typescript
class AuthorizationError extends DeIterateError {
  constructor(message: string, code?: string, requestId?: string);
}

NotFoundError

Thrown when a resource is not found (404).

typescript
class NotFoundError extends DeIterateError {
  constructor(message: string, requestId?: string);
}

Example

typescript
try {
  await client.risks.get('non-existent-id');
} catch (error) {
  if (error instanceof NotFoundError) {
    console.log('Risk not found');
  }
}

RateLimitError

Thrown when rate limit is exceeded (429).

typescript
class RateLimitError extends DeIterateError {
  public readonly retryAfter: number;

  constructor(message: string, retryAfter: number, requestId?: string);
}

Properties

PropertyTypeDescription
retryAfternumberMilliseconds to wait before retrying

Example

typescript
try {
  await client.risks.list();
} catch (error) {
  if (error instanceof RateLimitError) {
    console.log(`Rate limited. Retry after ${error.retryAfter}ms`);
    await sleep(error.retryAfter);
    // Retry the request
  }
}

ConflictError

Thrown when there's a conflict (409).

typescript
class ConflictError extends DeIterateError {
  constructor(message: string, requestId?: string);
}

InternalError

Thrown for server errors (500).

typescript
class InternalError extends DeIterateError {
  constructor(message: string, requestId?: string);
}

Error Handling Pattern

typescript
import {
  DeIterateError,
  ValidationError,
  AuthenticationError,
  AuthorizationError,
  NotFoundError,
  RateLimitError,
  ConflictError,
  InternalError,
} from '@deiterate/sdk';

async function handleRequest() {
  try {
    const risk = await client.risks.get('risk-123');
    return risk;
  } catch (error) {
    if (error instanceof ValidationError) {
      console.error('Validation failed:', error.fieldErrors);
    } else if (error instanceof AuthenticationError) {
      console.error('Authentication failed - check API key');
    } else if (error instanceof AuthorizationError) {
      console.error('Not authorized to access this resource');
    } else if (error instanceof NotFoundError) {
      console.error('Resource not found');
    } else if (error instanceof RateLimitError) {
      console.error(`Rate limited. Retry after ${error.retryAfter}ms`);
    } else if (error instanceof ConflictError) {
      console.error('Resource conflict');
    } else if (error instanceof InternalError) {
      console.error('Server error');
    } else if (error instanceof DeIterateError) {
      console.error(`API error: ${error.code} - ${error.message}`);
    } else {
      throw error; // Re-throw unknown errors
    }
  }
}

Released under the MIT License.