LeXtract.Error (lextract v0.1.2)

View Source

Main error aggregator for LeXtract using Splode.

This module provides structured error handling with four error classes:

  • :invalid - Validation and format errors
  • :processing - Processing pipeline failures
  • :external - External service/resource failures
  • :unknown - Unexpected errors

Examples

iex> error = LeXtract.Error.Invalid.Format.exception(format_string: "xml")
iex> LeXtract.Error.splode_error?(error)
true

iex> errors = [
...>   LeXtract.Error.Processing.Parsing.exception(format: :json, reason: "unexpected token"),
...>   LeXtract.Error.Invalid.Config.exception(errors: [])
...> ]
iex> class_error = LeXtract.Error.to_class(errors)
iex> match?(%LeXtract.Error.Invalid{}, class_error)
true

Summary

Functions

Raises an error if the result is an error, otherwise returns the result

Types

class()

@type class() :: %{
  :__struct__ => class_module(),
  :__exception__ => true,
  :errors => [t()],
  :class => error_class(),
  :bread_crumbs => [String.t()],
  :vars => Keyword.t(),
  :stacktrace => Splode.Stacktrace.t() | nil,
  :context => map(),
  optional(atom()) => any()
}

class_module()

@type class_module() ::
  LeXtract.Error.Unknown
  | LeXtract.Error.External
  | LeXtract.Error.Processing
  | LeXtract.Error.Invalid

error_class()

@type error_class() :: :unknown | :external | :processing | :invalid

t()

@type t() :: %{
  :__struct__ => module(),
  :__exception__ => true,
  :class => error_class(),
  :bread_crumbs => [String.t()],
  :vars => Keyword.t(),
  :stacktrace => Splode.Stacktrace.t() | nil,
  :context => map(),
  optional(atom()) => any()
}

Functions

splode_error?(arg1, splode)

unwrap!(result, opts \\ nil)

Raises an error if the result is an error, otherwise returns the result

Alternatively, you can use the defsplode macro, which does this automatically.

Options

  • :error_opts - Options to pass to to_error/2 when converting the returned error
  • :unknown_error_opts - Options to pass to the unknown error if the function returns only :error. not necessary if your function always returns {:error, error}.

Examples

def function(arg) do

case do_something(arg) do
  :success -> :ok
  {:success, result} -> {:ok, result}
  {:error, error} -> {:error, error}
end

end

def function!(arg) do

YourErrors.unwrap!(function(arg))

end