ExLLM.Error (ex_llm v0.1.0)
View SourceStandardized error types and utilities for ExLLM.
This module defines consistent error patterns used throughout ExLLM to provide predictable error handling for library consumers.
Error Patterns
All functions should return either {:ok, result}
or {:error, reason}
.
Error reasons follow these patterns:
Simple Errors (atoms)
:not_found
- Resource not found:not_connected
- Connection not established:invalid_config
- Configuration is invalid:not_configured
- Required configuration missing:timeout
- Operation timed out:unauthorized
- Authentication failed
Complex Errors (tuples)
{:api_error, details}
- API call failed{:validation, field, message}
- Validation failed{:connection_failed, reason}
- Connection failure{:json_parse_error, reason}
- JSON parsing failed
Examples
# Simple error
{:error, :not_found}
# API error with details
{:error, {:api_error, %{status: 404, body: "Not found"}}}
# Validation error
{:error, {:validation, :name, "cannot be empty"}}
# Connection error
{:error, {:connection_failed, :timeout}}
Summary
Functions
Creates a standardized API error.
Creates a standardized connection failure error.
Checks if a value is an error tuple.
Extracts error reason from error tuple.
Creates a standardized JSON parsing error.
Creates a standardized validation error.
Types
@type error_reason() :: simple_error() | complex_error()
@type result(success_type) :: {:ok, success_type} | {:error, error_reason()}
@type simple_error() ::
:not_found
| :not_connected
| :invalid_config
| :not_configured
| :timeout
| :unauthorized
| :no_token_usage
| :server_not_found
| :unsupported_format
Functions
Creates a standardized API error.
Parameters
status
- HTTP status codebody
- Response body (string or map)
Returns
API error tuple
Creates a standardized connection failure error.
Parameters
reason
- Underlying failure reason
Returns
Connection error tuple
Checks if a value is an error tuple.
Parameters
value
- Value to check
Returns
Boolean indicating if value is an error tuple
@spec get_error_reason({:error, error_reason()} | term()) :: error_reason() | nil
Extracts error reason from error tuple.
Parameters
error_tuple
- Error tuple
Returns
Error reason or nil if not an error tuple
Creates a standardized JSON parsing error.
Parameters
reason
- JSON parsing failure reason
Returns
JSON parse error tuple
Creates a standardized validation error.
Parameters
field
- Field name (atom)message
- Error message (string)
Returns
Validation error tuple