Utility class for analyzing errors from various AI providers and mapping them to standardized error information. This class provides a consistent way to interpret errors across different provider SDKs.

ErrorAnalyzer

Since

2.47.0

Example

try {
const result = await provider.chat(params);
} catch (error) {
const errorInfo = ErrorAnalyzer.analyzeError(error, 'OpenAI');
if (errorInfo.canFailover) {
// Try another provider
}
}

Constructors

Methods

  • Analyzes an error from an AI provider and returns standardized error information. This method extracts relevant details from provider-specific error formats and maps them to a consistent structure for easier handling.

    Parameters

    • error: any

      The error object thrown by the provider SDK

    • Optional providerName: string

      Optional name of the provider for context

    Returns AIErrorInfo

    Standardized error information

    Static

    Example

    const errorInfo = ErrorAnalyzer.analyzeError(error, 'Anthropic');
    console.log(`Error type: ${errorInfo.errorType}`);
    console.log(`Can retry: ${errorInfo.severity !== 'Fatal'}`);
  • Private

    Determines whether an error can potentially be resolved by switching providers.

    Strategy: We're permissive with failover - most errors should allow trying another provider/model since vendors may use different status codes and error messages. Only block failover for clear client-side structural errors that won't be fixed by switching.

    Parameters

    Returns boolean

    True if failover might help, false otherwise

    Static

  • Private

    Determines the standardized error type based on status code and error properties. Uses a combination of HTTP status codes, error messages, and error class names.

    Parameters

    • error: any

      The error object to analyze

    • Optional statusCode: number

      The HTTP status code if available

    Returns AIErrorType

    The categorized error type

    Static

  • Private

    Determines the error severity based on the error type. This helps decide whether to retry immediately, wait, or fail permanently.

    Parameters

    Returns ErrorSeverity

    The severity level of the error

    Static

  • Private

    Extracts HTTP status code from various error object structures. Different provider SDKs store status codes in different locations.

    Parameters

    • error: any

      The error object to extract status code from

    Returns number

    The HTTP status code if found, undefined otherwise

    Static

  • Private

    Extracts the provider-specific error code from the error object. Different providers store error codes in different locations.

    Parameters

    • error: any

      The error object to extract provider code from

    Returns string

    The provider error code if found, undefined otherwise

    Static

  • Private

    Extracts suggested retry delay from error response. Looks for Retry-After headers and provider-specific retry delay fields.

    Parameters

    • error: any

      The error object to extract retry delay from

    Returns number

    Suggested retry delay in seconds, or undefined

    Static