ExLLM.Adapters.Shared.ErrorHandler (ex_llm v0.5.0)

View Source

Shared error handling utilities for ExLLM adapters.

Provides consistent error handling patterns across all provider adapters, including provider-specific error parsing and standardization.

Summary

Functions

Extract error message from various response formats.

Extract retry-after header value if present.

Handle provider-specific errors and convert to standard ExLLM errors.

Normalize error responses across providers for consistent handling.

Check if an error is retryable based on status code and error type.

Check if an error should trigger a retry based on provider-specific rules.

Functions

extract_error_message(arg1)

@spec extract_error_message(map()) :: String.t() | nil

Extract error message from various response formats.

get_retry_after(headers)

@spec get_retry_after([{String.t(), String.t()}]) :: integer() | nil

Extract retry-after header value if present.

handle_provider_error(provider, status, body)

@spec handle_provider_error(atom(), integer(), map() | String.t()) :: {:error, term()}

Handle provider-specific errors and convert to standard ExLLM errors.

Examples

# OpenAI-style error
ErrorHandler.handle_provider_error(:openai, 429, %{
  "error" => %{
    "type" => "rate_limit_error",
    "message" => "Rate limit exceeded"
  }
})

# Anthropic-style error  
ErrorHandler.handle_provider_error(:anthropic, 400, %{
  "error" => %{
    "type" => "invalid_request_error",
    "message" => "max_tokens required"
  }
})

normalize_error(provider, status, body)

@spec normalize_error(atom(), integer(), term()) :: {:error, term()}

Normalize error responses across providers for consistent handling.

Returns a standardized error tuple.

retryable_error?(status, arg2)

@spec retryable_error?(integer(), map() | term()) :: boolean()

Check if an error is retryable based on status code and error type.

should_retry?(arg1, status, arg3)

@spec should_retry?(atom(), integer(), term()) :: boolean()

Check if an error should trigger a retry based on provider-specific rules.