Lab42.Message v0.1.0 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

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

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_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

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(15)> messages =
...(15)>   []
...(15)>   |> add_error("error1", 1)
...(15)>   |> add_info("info2", 2)
...(15)>   |> add_warning("warning3", 3)
...(15)> 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(16)> messages =
...(16)>   []
...(16)>   |> add_error("error1", 1)
...(16)>   |> add_info("info2", 2)
...(16)>   |> add_debug("debug3", 3)
...(16)> 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(17)> messages =
...(17)>   []
...(17)>   |> add_error("error1", 1)
...(17)>   |> add_info("info2", 2)
...(17)>   |> add_debug("debug3", 3)
...(17)> messages(messages, :all)
[ {:error, "error1", 1}, {:info, "info2", 2}, {:debug, "debug3", 3} ]
Link to this function

result(messages, value)

View Source
result(ts(), any()) :: result_t()

Wrap a value and error messages into a result tuple

iex(18)> result([], 42)
{:ok, 42, []}

Messages of severity warning or less still deliver a :ok result

iex(19)> messages = []
...(19)>   |> add_debug("hello", 1)
...(19)>   |> add_info("hello again", 2)
...(19)>   |> add_warning("world", 3)
...(19)> {:ok, "result", ^messages} = result(messages, "result")
...(19)> true
true
Link to this function

severity_value(message_or_severity)

View Source

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

  iex(20)> severity_value(:debug)
  0

The function extracts the severity from a message if necessary

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