Flux Error v0.0.1 FluxError.Messages behaviour View Source

Module responsible to extract error messages.

The main approach to extract is done by defining a module which extends FluxError.Messages and implements c:get_messages/2 function. For example:

defmodule MyApp.ErrorMessages do
  use FluxError.Messages

  @impl FluxError.Messages
  def get_message(:invalid_type, opts) do
    "the type '" <> key(:type, opts) <> "' is invalid. " <>
      "Valid types are: " <> key(:valid_types, opts)
  end

  def get_message(:missing_type, _opts) do
    "the payload must have the key 'type'"
  end

  def get_message(_reason, _opts) do
    "unknown error"
  end
end

The example above will generate 3 messages:

  • If :reason is :invalid_type, it will generate the following message (with default options):The type '%{type}' is invalid. Valid types are: %{valid_types}

  • If :reason is :missing_type, it will generate the following message: the payload must have the key 'type'

  • Otherwise, it will generate the default message: unknown error

Using FluxError.message/1 with error :reason as :invalid_type and :metadata containing both :type and :valid_types keys. It will inject the metadata values into the message generated.

The key/2 function generate the injection string based on :format opts:

You can create additional functions and options to aid the generation of messages.

Link to this section Summary

Link to this section Callbacks

Link to this callback

get_message(reason, opts)

View Source
get_message(reason :: atom(), opts :: keyword()) :: String.t()