Flux Error v0.0.1 FluxError exception View Source

Exception module to improve error management.

Link to this section Summary

Types

t()

FluxError exception struct.

Functions

Log the error, returning the FluxError.t/0.

Extract the error message.

Generate a map/0 based on error data.

Link to this section Types

Link to this type

t()

View Source (since 0.0.1)
t() :: %FluxError{__exception__: term(), metadata: map(), reason: atom()}

FluxError exception struct.

The :reason will be used to identify the error message.

The :metadata will be used to inject data into the error message.

Link to this section Functions

Link to this function

from(error, reason \\ :unhandled_error, metadata \\ [])

View Source (since 0.0.1)
from(Exception.t() | atom(), atom(), keyword()) :: t()

Create FluxError.t/0 from an external error.

Parameters

Examples

iex> FluxError.from(:external_failure, :external_module_failed, module: :external)
%FluxError{
  reason: :external_module_failed,
  metadata: %{
    detail: :external_failure,
    module: :external
  }
}
Link to this function

log(error, stacktrace)

View Source (since 0.0.1)
log(Exception.t() | atom(), Exception.stacktrace()) :: t()

Log the error, returning the FluxError.t/0.

It will create a FluxError.t/0 if error is an atom/0 or other Exception.t/0 using FluxError.from/3.

The log will have two metadata values:

  • :error_metadata - The inspection of the FluxError.t/0 :metadata key.
  • :error_stacktrace - The error stacktrace.

Parameters

Examples

iex> try do
...>   throw(:failed)
...> catch
...>   error -> FluxError.log(error, __STACKTRACE__)
...> end
%FluxError{
  reason: :unhandled_error,
  metadata: %{
    detail: :failed
  }
}
Link to this function

message(error)

View Source (since 0.0.1)
message(Exception.t() | atom()) :: String.t()

Extract the error message.

It will create a FluxError.t/0 if error is an atom/0 or other Exception.t/0 using FluxError.from/3.

The message is extracted based on :messages_module app config. Check FluxError for more information.

Parameters

Examples

iex> FluxError.message(:failed)
"triggered unhandled error"
Link to this function

new(reason, metadata \\ [])

View Source (since 0.0.1)
new(atom(), keyword()) :: t()

Create FluxError.t/0.

Parameters

  • reason - An atom/0 identifying the error.

  • metadata - A keyword/0 with detailed data about the error. Defaults to empty list/0.

Examples

iex> FluxError.new(:failed, level: :severe)
%FluxError{
  reason: :failed,
  metadata: %{
    level: :severe
  }
}
Link to this function

to_map(error, opts \\ [])

View Source (since 0.0.1)
to_map(Exception.t() | atom(), keyword() | nil) :: map()

Generate a map/0 based on error data.

It will create a FluxError.t/0 if error is an atom/0 or other Exception.t/0 using FluxError.from/3.

Parameters

Examples

iex> FluxError.to_map(:failed)
%{
  error: true,
  id: :unhandled_error,
  message: "triggered unhandled error",
  message_raw: "triggered unhandled error",
  metadata: %{
    detail: :failed
  }
}