Raxol.Core.ErrorHandler (Raxol v2.0.1)

View Source

Centralized error handling module for the Raxol application.

Provides consistent error handling, logging, and recovery mechanisms across all modules in the system.

Features

  • Standardized error types and messages
  • Automatic error logging with context
  • Graceful degradation strategies
  • Error recovery mechanisms
  • Telemetry integration for error monitoring

Usage

import Raxol.Core.ErrorHandler

# Handle errors with automatic logging
with_error_handling :file_operation do
  File.read!("/path/to/file")
end

# Custom error recovery
handle_error {:error, :not_found}, default: "default_value"

Summary

Functions

Creates a standardized error tuple.

Executes a function with error handling.

Handles an error result with optional recovery.

Creates a supervisor-friendly error handler for GenServer processes.

Converts various error formats to standardized format.

Chains multiple operations with error handling.

Wraps a function call with error handling and logging.

Types

error_context()

@type error_context() :: map()

error_result()

@type error_result() ::
  {:error, error_type(), String.t()}
  | {:error, error_type(), String.t(), error_context()}

error_severity()

@type error_severity() :: :debug | :info | :warning | :error | :critical

error_type()

@type error_type() ::
  :validation
  | :runtime
  | :system
  | :network
  | :permission
  | :not_found
  | :timeout

Functions

error(type, message, context \\ %{})

Creates a standardized error tuple.

Examples

error(:validation, "Invalid email format")
error(:not_found, "User not found", %{user_id: 123})

execute_pipeline(steps)

execute_with_handling(operation, opts, fun)

Executes a function with error handling.

handle_error(error, opts)

Handles an error result with optional recovery.

Examples

result
|> handle_error(default: "fallback")
|> handle_error(with: fn error -> recover(error) end)

handle_genserver_error(error, state, module)

Creates a supervisor-friendly error handler for GenServer processes.

log_error(operation, error, context \\ %{}, severity \\ :error)

Logs an error with context.

normalize_error(error)

Converts various error formats to standardized format.

pipeline(list)

(macro)

Chains multiple operations with error handling.

Examples

pipeline do
  step :validate_input, &validate/1
  step :process_data, &process/1
  step :save_result, &save/1
end

with_error_handling(operation, opts \\ [], list)

(macro)

Wraps a function call with error handling and logging.

Options

  • :context - Additional context to log with errors
  • :severity - Error severity level (default: :error)
  • :fallback - Fallback value on error
  • :retry - Number of retry attempts (default: 0)
  • :retry_delay - Delay between retries in ms (default: 1000)

Examples

with_error_handling(:database_query, context: %{user_id: 123}) do
  Repo.get!(User, user_id)
end