View Source Avalanche.Error exception (Avalanche v0.13.0)

Common application error.

Summary

Types

t()

The exception type

Functions

Builds an Error struct with a reason of :application_error.

Formats a Error for printing/logging.

Builds an Error struct with reason and message deived from the given http status.

Returns the Error message.

Creates a new Error from a message string or another error

Builds an Error struct.

Types

@type meta() :: map() | keyword()
@type t() :: %Avalanche.Error{
  __exception__: true,
  message: String.t(),
  meta: meta(),
  original_error: any(),
  reason: atom(),
  stacktrace: nil | Exception.stacktrace()
}

The exception type

Functions

Link to this function

application_error(message, meta \\ %{})

View Source
@spec application_error(binary(), meta()) :: t()

Builds an Error struct with a reason of :application_error.

Examples:

iex> alias Avalanche.Error
iex> Error.application_error("Bad Things", %{data: "things"})
%Error{__exception__: true, message: "Bad Things", meta: %{data: "things"}, reason: :application_error}
@spec format(t()) :: binary()

Formats a Error for printing/logging.

This returns a verbose, multi-line string.

Examples:

iex> alias Avalanche.Error
iex> RuntimeError.exception("Failed!") |> Error.new() |> Error.format()
~s<application_error: Failed!
meta: []
original_error: ** (RuntimeError) Failed!>
iex> "Failed!" |> Error.new() |> Error.format()
~s<application_error: Failed!
meta: []
original_error: nil>
iex> 123 |> Error.new() |> Error.format() =~ ~r/original_error: 123/
true
iex> :bad |> Error.new() |> Error.format() =~ ~r/bad/
true
Link to this function

http_status(status, meta \\ %{})

View Source
@spec http_status(integer(), meta()) :: t()

Builds an Error struct with reason and message deived from the given http status.

Examples:

iex> alias Avalanche.Error
iex> Error.http_status(404, %{data: "things"})
%Error{__exception__: true, message: "Not Found", meta: %{data: "things"}, reason: :not_found}
@spec message(t()) :: binary()

Returns the Error message.

Examples:

iex> alias Avalanche.Error
iex> error = Error.application_error("Bad Things", %{data: "things"})
iex> Error.message(error)
"application_error: Bad Things"
@spec new(any()) :: t()

Creates a new Error from a message string or another error

Examples:

# A string will be used as message
iex> alias Avalanche.Error
iex> Error.new("These are the error message")
%Error{message: "These are the error message"}
# Error structs are returned unchanged
iex> Error.new(%Error{reason: :some_reason})
%Error{reason: :some_reason}
# Atoms will be used as reason
iex> Error.new(:some_reason)
%Error{reason: :some_reason}
# Anything else will be used as the `original_error`
iex> Error.new(%RuntimeError{message: "oops!"})
%Error{message: "oops!", original_error: %RuntimeError{message: "oops!"}}
Link to this function

new(reason, message, meta \\ %{})

View Source
@spec new(atom(), String.t(), meta()) :: t()

Builds an Error struct.

Examples:

iex> alias Avalanche.Error
iex> Error.new(:bad, "Bad Things", %{data: "things"})
%Error{__exception__: true, message: "Bad Things", meta: %{data: "things"}, reason: :bad}