Nous.Errors.Base (nous v0.17.0)

View Source

Shared construction for Nous exception modules.

use Nous.Errors.Base, fields: [...] generates the defexception and the two exception/1 clauses every Nous error shares: a keyword form taking the declared fields plus an optional :message override, and a bare-binary message form.

The using module supplies a private default_message/1, which receives the map of declared fields and returns the message used when :message is not given.

defmodule MyError do
  use Nous.Errors.Base, fields: [:reason]

  @type t :: %__MODULE__{message: String.t(), reason: String.t() | nil}

  defp default_message(%{reason: reason}) do
    "It broke" <> if(reason, do: ": #{reason}", else: "")
  end
end