Mojentic.Error (Mojentic v1.2.0)

Copy Markdown View Source

Standardized error types and helpers for the Mojentic framework.

Follows Elixir conventions of returning {:ok, result} or {:error, reason} tuples. Exceptions are reserved for truly exceptional situations.

Error Reasons

Atoms (simple errors)

  • :invalid_response - Response from LLM gateway could not be parsed
  • :model_not_supported - Requested model is not available
  • :timeout - Operation timed out

Tagged Tuples (errors with context)

  • {:gateway_error, message} - LLM gateway error
  • {:api_error, message} - API-specific error
  • {:http_error, status} - HTTP request failed with status code
  • {:request_failed, reason} - Network request failed
  • {:tool_error, message} - Tool execution error
  • {:config_error, message} - Invalid configuration
  • {:serialization_error, message} - JSON serialization/deserialization error

Summary

Functions

Creates an API error tuple.

Creates a config error tuple.

Formats an error reason into a human-readable string.

Creates a gateway error tuple.

Creates an HTTP error tuple.

Creates an invalid response error tuple.

Creates a model not supported error tuple.

Creates a request failed error tuple.

Creates a serialization error tuple.

Creates a timeout error tuple.

Creates a tool error tuple.

Types

error_reason()

@type error_reason() :: simple_error() | tagged_error() | String.t()

result(success_type)

@type result(success_type) :: {:ok, success_type} | {:error, error_reason()}

simple_error()

@type simple_error() :: :invalid_response | :model_not_supported | :timeout

tagged_error()

@type tagged_error() ::
  {:gateway_error, String.t()}
  | {:api_error, String.t()}
  | {:http_error, integer()}
  | {:request_failed, term()}
  | {:tool_error, String.t()}
  | {:config_error, String.t()}
  | {:serialization_error, String.t()}

Functions

api_error(message)

@spec api_error(String.t()) :: {:error, {:api_error, String.t()}}

Creates an API error tuple.

Examples

iex> Mojentic.Error.api_error("Rate limit exceeded")
{:error, {:api_error, "Rate limit exceeded"}}

config_error(message)

@spec config_error(String.t()) :: {:error, {:config_error, String.t()}}

Creates a config error tuple.

Examples

iex> Mojentic.Error.config_error("Missing API key")
{:error, {:config_error, "Missing API key"}}

format_error(message)

@spec format_error(error_reason()) :: String.t()

Formats an error reason into a human-readable string.

Examples

iex> Mojentic.Error.format_error({:gateway_error, "Connection failed"})
"Gateway error: Connection failed"

iex> Mojentic.Error.format_error(:invalid_response)
"Invalid response"

iex> Mojentic.Error.format_error("Custom error")
"Custom error"

gateway_error(message)

@spec gateway_error(String.t()) :: {:error, {:gateway_error, String.t()}}

Creates a gateway error tuple.

Examples

iex> Mojentic.Error.gateway_error("Connection failed")
{:error, {:gateway_error, "Connection failed"}}

http_error(status)

@spec http_error(integer()) :: {:error, {:http_error, integer()}}

Creates an HTTP error tuple.

Examples

iex> Mojentic.Error.http_error(404)
{:error, {:http_error, 404}}

invalid_response()

@spec invalid_response() :: {:error, :invalid_response}

Creates an invalid response error tuple.

Examples

iex> Mojentic.Error.invalid_response()
{:error, :invalid_response}

model_not_supported()

@spec model_not_supported() :: {:error, :model_not_supported}

Creates a model not supported error tuple.

Examples

iex> Mojentic.Error.model_not_supported()
{:error, :model_not_supported}

request_failed(reason)

@spec request_failed(term()) :: {:error, {:request_failed, term()}}

Creates a request failed error tuple.

Examples

iex> Mojentic.Error.request_failed(:timeout)
{:error, {:request_failed, :timeout}}

serialization_error(message)

@spec serialization_error(String.t()) :: {:error, {:serialization_error, String.t()}}

Creates a serialization error tuple.

Examples

iex> Mojentic.Error.serialization_error("Invalid JSON")
{:error, {:serialization_error, "Invalid JSON"}}

timeout()

@spec timeout() :: {:error, :timeout}

Creates a timeout error tuple.

Examples

iex> Mojentic.Error.timeout()
{:error, :timeout}

tool_error(message)

@spec tool_error(String.t()) :: {:error, {:tool_error, String.t()}}

Creates a tool error tuple.

Examples

iex> Mojentic.Error.tool_error("Invalid parameters")
{:error, {:tool_error, "Invalid parameters"}}