View Source Bonfire.Common.Errors (Bonfire v0.9.10-classic-beta.169)

Helpers for handling error messages and exceptions

Summary

Functions

Logs a debug message with exception and stacktrace information.

Logs a debug message with optional exception and stacktrace information.

Turns various kinds of errors into an error message string. Used to format errors in a way that can be easily read by the user.

Normalizes and formats any throw/error/exit. The message is formatted and displayed in the same format as used by Elixir's CLI.

Receives a module, function, and arity and formats it as shown in stacktraces. The arity may also be a list of arguments.

Formats the stacktrace. A stacktrace must be given as an argument. If not, the stacktrace is retrieved from Process.info/2.

Receives a stacktrace entry and formats it into a string.

Maps an error tuple to a new value using the provided function.

Applies change_fn if the first parameter is an {:ok, val} tuple, else returns the value.

Replaces the error value in an error tuple with a new value.

Functions

Link to this function

debug_banner_with_trace(kind, exception, stacktrace, opts \\ [])

View Source
Link to this function

debug_exception(msg, exception \\ nil, stacktrace \\ nil, kind \\ :error, opts \\ [])

View Source

Logs a debug message with exception and stacktrace information.

Examples

iex> debug_exception("An error occurred", %RuntimeError{message: "error"}, nil, :error, [])
# Output: An error occurred: %RuntimeError{message: "error"}
{:error, "An error occurred"}
Link to this function

debug_log(msg, exception \\ nil, stacktrace \\ nil, kind \\ :error, msg_text \\ nil)

View Source

Logs a debug message with optional exception and stacktrace information.

Examples

> debug_log("A debug message", %RuntimeError{message: "error"}, nil, :error)
# Output: A debug message: %RuntimeError{message: "error"}

> debug_log("A debug message", nil, nil, :info)
# Output: A debug message: nil

Turns various kinds of errors into an error message string. Used to format errors in a way that can be easily read by the user.

Examples

iex> error_msg([{:error, "something went wrong"}])
["something went wrong"]

iex> error_msg(%{message: "custom error"})
"custom error"

iex> error_msg(:some_other_error)
":some_other_error"
Link to this function

format_banner(kind, exception, stacktrace \\ [], opts \\ [])

View Source

Normalizes and formats any throw/error/exit. The message is formatted and displayed in the same format as used by Elixir's CLI.

The third argument is the stacktrace which is used to enrich a normalized error with more information. It is only used when the kind is an error.

Examples

iex> format_banner(:error, %RuntimeError{message: "error"})
"** Elixir.RuntimeError: error"

iex> format_banner(:throw, :some_reason)
"** (throw) :some_reason"

iex> format_banner(:exit, :some_reason)
"** (exit) :some_reason"

> format_banner({:EXIT, self()}, :some_reason)
"** (EXIT from #PID<0.780.0>) :some_reason"
Link to this function

format_mfa(module, fun, arity)

View Source

Receives a module, function, and arity and formats it as shown in stacktraces. The arity may also be a list of arguments.

Anonymous functions are reported as -func/arity-anonfn-count-, where func is the name of the enclosing function. Convert to "anonymous fn in func/arity"

Examples

iex> format_mfa(Foo, :bar, 1)
{"Foo", "bar", "Foo.bar/1"}

iex> format_mfa(Foo, :bar, [])
{"Foo", "bar", "Foo.bar()"}

iex> Exception.format_mfa(nil, :bar, [])
"nil.bar()"
Link to this function

format_stacktrace(trace \\ nil, opts \\ [])

View Source

Formats the stacktrace. A stacktrace must be given as an argument. If not, the stacktrace is retrieved from Process.info/2.

TODO: consolidate/reuse with similar function in Untangle?

Examples

> format_stacktrace([{MyModule, :my_fun, 1, [file: 'my_file.ex', line: 42]}], [])
"my_file.ex:42: MyModule.my_fun/1"

> format_stacktrace(nil, [])
"stacktrace here..."
Link to this function

format_stacktrace_entry(entry, opts \\ [])

View Source

Receives a stacktrace entry and formats it into a string.

Examples

iex> format_stacktrace_entry({MyModule, :my_fun, 1, [file: 'my_file.ex', line: 42]}, [])
"my_file.ex:42: MyModule.my_fun/1"

> format_stacktrace_entry({fn -> :ok end, 0, [file: 'another_file.ex', line: 7]}, [])
"another_file.ex:7: some_fun/2"
Link to this function

format_stacktrace_entry_sliced(entry, opts)

View Source

Maps an error tuple to a new value using the provided function.

Examples

iex> map_error({:error, :some_error}, &(&1 |> to_string()))
"some_error"

iex> map_error(42, &(&1 * 2))
42
Link to this function

maybe_ok_error(other, change_fn)

View Source
@spec maybe_ok_error(any(), any()) :: any()

Applies change_fn if the first parameter is an {:ok, val} tuple, else returns the value.

Examples

iex> maybe_ok_error({:ok, 42}, &(&1 * 2))
{:ok, 84}

iex> maybe_ok_error({:error, :some_error}, &(&1 * 2))
{:error, :some_error}

iex> maybe_ok_error(42, &(&1 * 2))
42
Link to this function

replace_error(other, value)

View Source

Replaces the error value in an error tuple with a new value.

Examples

iex> replace_error({:error, :old_value}, :new_value)
{:error, :new_value}

iex> replace_error(42, :new_value)
42