Lab42.Message v0.1.2 Lab42.Message View Source

A container for error messages.

Defining some severities.

Create results depending on error messages.

Convenience functions for adding, filtering and sorting messages.

Link to this section Summary

Functions

Create a message with severity :critical and add in front of other messages

Create a message with severity :debug and add in front of other messages

Create a message with severity :error and add in front of other messages

Create a message with severity :fatal and add in front of other messages

Create a message with severity :info and add in front of other messages

Create a message with severity :warning and add in front of other messages

Extract a value from an ok result

Create a message with severity :critical

Create a message with severity :debug

Create a message with severity :error

Create a message with severity :fatal

Create a message with severity :info

Create a message with severity :warning

Returns the maximum priority of messages A list of messages can be passed in

Extract messages from a list of messages into a library agnositic form as triples. As all the add_* functions create a list in reverse order, this function also rereverses the message tuples.

Wrap a value and error messages into a result tuple, messages themselves are converted to message tuples as with messages. Also warnings still deliver an :ok reesult.œ

Assigns to each severity a numerical value, where a higher value indicates a higher severity.

Link to this section Types

Link to this type

location_t()

View Source
location_t() :: any()
Link to this type

message_list_t()

View Source
message_list_t() :: [t() | message_t()]
Link to this type

message_ts()

View Source
message_ts() :: [message_t()]
Link to this type

result_t()

View Source
result_t() :: {:ok | :error, any(), [message_t()]}
Link to this type

severity_t()

View Source
severity_t() :: :debug | :info | :warning | :error | :critical | :fatal
Link to this type

t()

View Source
t() :: %Lab42.Message{
  location: location_t(),
  message: String.t(),
  severity: severity_t()
}

Link to this section Functions

Link to this function

add_critical(messages, message, location)

View Source
add_critical(ts(), String.t(), any()) :: ts()

Create a message with severity :critical and add in front of other messages

iex(10)> add_critical([], "Just a critical message", {1, 3})
[%Lab42.Message{message: "Just a critical message", severity: :critical, location: {1, 3}}]
Link to this function

add_debug(messages, message, location)

View Source
add_debug(ts(), String.t(), any()) :: ts()

Create a message with severity :debug and add in front of other messages

iex(2)> add_debug([], "Just a debug message", {1, 3})
[%Lab42.Message{message: "Just a debug message", severity: :debug, location: {1, 3}}]
Link to this function

add_error(messages, message, location)

View Source
add_error(ts(), String.t(), any()) :: ts()

Create a message with severity :error and add in front of other messages

iex(8)> add_error([], "Just a error message", {1, 3})
[%Lab42.Message{message: "Just a error message", severity: :error, location: {1, 3}}]
Link to this function

add_fatal(messages, message, location)

View Source
add_fatal(ts(), String.t(), any()) :: ts()

Create a message with severity :fatal and add in front of other messages

iex(12)> add_fatal([], "Just a fatal message", {1, 3})
[%Lab42.Message{message: "Just a fatal message", severity: :fatal, location: {1, 3}}]
Link to this function

add_info(messages, message, location)

View Source
add_info(ts(), String.t(), any()) :: ts()

Create a message with severity :info and add in front of other messages

iex(4)> add_info([], "Just a info message", {1, 3})
[%Lab42.Message{message: "Just a info message", severity: :info, location: {1, 3}}]
Link to this function

add_warning(messages, message, location)

View Source
add_warning(ts(), String.t(), any()) :: ts()

Create a message with severity :warning and add in front of other messages

iex(6)> add_warning([], "Just a warning message", {1, 3})
[%Lab42.Message{message: "Just a warning message", severity: :warning, location: {1, 3}}]
Link to this function

extract!(result)

View Source
extract!(result_t()) :: any()

Extract a value from an ok result

iex(13)> extract!(result([], 42))
42

However, extracting from an error result is not possible

iex(14)> extract!({:error, 42, []})
** (FunctionClauseError) no function clause matching in Lab42.Message.extract!/1
Link to this function

make_critical(message, location)

View Source
make_critical(String.t(), any()) :: t()

Create a message with severity :critical

iex(11)> make_critical("Just a critical message", {1, 3})
%Lab42.Message{message: "Just a critical message", severity: :critical, location: {1, 3}}
Link to this function

make_debug(message, location)

View Source
make_debug(String.t(), any()) :: t()

Create a message with severity :debug

iex(3)> make_debug("Just a debug message", {1, 3})
%Lab42.Message{message: "Just a debug message", severity: :debug, location: {1, 3}}
Link to this function

make_error(message, location)

View Source
make_error(String.t(), any()) :: t()

Create a message with severity :error

iex(9)> make_error("Just a error message", {1, 3})
%Lab42.Message{message: "Just a error message", severity: :error, location: {1, 3}}
Link to this function

make_fatal(message, location)

View Source
make_fatal(String.t(), any()) :: t()

Create a message with severity :fatal

iex(13)> make_fatal("Just a fatal message", {1, 3})
%Lab42.Message{message: "Just a fatal message", severity: :fatal, location: {1, 3}}
Link to this function

make_info(message, location)

View Source
make_info(String.t(), any()) :: t()

Create a message with severity :info

iex(5)> make_info("Just a info message", {1, 3})
%Lab42.Message{message: "Just a info message", severity: :info, location: {1, 3}}
Link to this function

make_warning(message, location)

View Source
make_warning(String.t(), any()) :: t()

Create a message with severity :warning

iex(7)> make_warning("Just a warning message", {1, 3})
%Lab42.Message{message: "Just a warning message", severity: :warning, location: {1, 3}}
Link to this function

max_severity(message_list, opts \\ [])

View Source
max_severity(message_list_t(), Keyword.t()) :: severity_t()

Returns the maximum priority of messages A list of messages can be passed in

iex(15)> messages =
...(15)>   []
...(15)>   |> add_error("error1", 1)
...(15)>   |> add_info("info2", 2)
...(15)>   |> add_warning("warning3", 3)
...(15)> max_severity(messages)
:error

However a list of message tuples is also allowed

iex(16)> messages =
...(16)>   []
...(16)>   |> add_error("error1", 1)
...(16)>   |> add_fatal("fatal2", 2)
...(16)>   |> add_warning("warning3", 3)
...(16)>   |> messages()
...(16)> max_severity(messages)
:fatal

In accordance of the robustness principle the last can even be mixed

iex(17)> messages =
...(17)>   []
...(17)>   |> add_error("what an error", 42)
...(17)>   |> add_info("what an info", 42)
...(17)> max_severity([{:critical, "", nil}|messages])
:critical

And last, but not least it might be convenient to get the severity_value instead of the symbolic severity

iex(18)> messages =
...(18)>   []
...(18)>   |> add_error("what an error", 42)
...(18)>   |> add_info("what an info", 42)
...(18)> max_severity([{:critical, "", nil}|messages], value: true)
4
Link to this function

messages(messages, options \\ [])

View Source
messages(ts(), Keyword.t() | :all) :: message_ts()

Extract messages from a list of messages into a library agnositic form as triples. As all the add_* functions create a list in reverse order, this function also rereverses the message tuples.

iex(19)> messages =
...(19)>   []
...(19)>   |> add_error("error1", 1)
...(19)>   |> add_info("info2", 2)
...(19)>   |> add_warning("warning3", 3)
...(19)> messages(messages)
[ {:error, "error1", 1}, {:warning, "warning3", 3} ]

As you can see only messages with severity of warning and up are returned.

One can of course get messages with less severity too:

iex(20)> messages =
...(20)>   []
...(20)>   |> add_error("error1", 1)
...(20)>   |> add_info("info2", 2)
...(20)>   |> add_debug("debug3", 3)
...(20)> messages(messages, severity: :info)
[ {:error, "error1", 1}, {:info, "info2", 2} ]

And, eventually, for your convenience, instead of severity: :debug a shorter and more expressive :all can be passed in

iex(21)> messages =
...(21)>   []
...(21)>   |> add_error("error1", 1)
...(21)>   |> add_info("info2", 2)
...(21)>   |> add_debug("debug3", 3)
...(21)> messages(messages, :all)
[ {:error, "error1", 1}, {:info, "info2", 2}, {:debug, "debug3", 3} ]
Link to this function

result(messages, value, options \\ [])

View Source
result(ts(), any(), Keyword.t()) :: result_t()

Wrap a value and error messages into a result tuple, messages themselves are converted to message tuples as with messages. Also warnings still deliver an :ok reesult.œ

iex(22)> messages = []
...(22)>   |> add_debug("hello", 1)
...(22)>   |> add_info("hello again", 2)
...(22)>   |> add_warning("world", 3)
...(22)> result(messages, "result")
{:ok, "result", [{:warning, "world", 3}]}

However the presence of errors or worse returns an :error result. N.B. that the input can be a mixture of Lab42.Message structs and agnostic tuples.

iex(23)> messages = [{:fatal, "that was not good", 0}]
...(23)>   |> add_debug("hello", 1)
...(23)> result(messages, "result")
{:error, "result", [{:fatal, "that was not good", 0}]}

As with messages one can control what level of errors shall be included, here is an example where warnings are surpressed

iex(24)> messages = []
...(24)>   |> add_error("hello", 1)
...(24)>   |> add_info("hello again", 2)
...(24)>   |> add_warning("world", 3)
...(24)> result(messages, 42, severity: :error)
{:error, 42, [{:error, "hello", 1}]}
Link to this function

severity_value(message_or_severity)

View Source
severity_value(t() | severity_t() | message_t()) :: number()

Assigns to each severity a numerical value, where a higher value indicates a higher severity.

  iex(24)> severity_value(:debug)
  0

The function extracts the severity from a message if necessary

  iex(25)> severity_value(%Lab42.Message{severity: :error})
  3