BCUtils.Errors (bc_utils v0.11.0)

Standardized error handling for BCUtils modules.

This module provides common error types and utilities for consistent error handling across the BCUtils library.

Error Types

Summary

Functions

Creates a formatted error report for logging or debugging.

Extracts error message from any BCUtils error struct.

Wraps a function result in an ok/error tuple if not already wrapped.

Types

error_context()

@type error_context() :: map()

error_reason()

@type error_reason() :: String.t()

Functions

format_error(error)

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

Creates a formatted error report for logging or debugging.

Examples

iex> error = BCUtils.Errors.ConfigurationError.new("Invalid config", :theme, "atom", "string")
iex> BCUtils.Errors.format_error(error)
"ConfigurationError: Invalid config\nConfig Key: theme\nExpected: atom\nReceived: \"string\""

message(error)

@spec message(struct()) :: String.t()

Extracts error message from any BCUtils error struct.

Examples

iex> error = BCUtils.Errors.InvalidInputError.new("Bad parameter")
iex> BCUtils.Errors.message(error)
"Bad parameter"

wrap_result(result)

@spec wrap_result(any()) :: {:ok, any()} | {:error, any()}

Wraps a function result in an ok/error tuple if not already wrapped.

Examples

iex> BCUtils.Errors.wrap_result("success")
{:ok, "success"}

iex> BCUtils.Errors.wrap_result({:error, "failed"})
{:error, "failed"}