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
severity_t()
View Sourceseverity_t() :: :debug | :info | :warning | :error | :critical | :fatal
t()
View Sourcet() :: %Lab42.Message{ location: location_t(), message: String.t(), severity: severity_t() }
Link to this section Functions
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}}]
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}}]
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}}]
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}}]
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}}]
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}}]
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
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}}
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}}
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}}
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}}
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}}
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}}
max_severity(message_list, opts \\ [])
View Sourcemax_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
messages(messages, options \\ [])
View Sourcemessages(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} ]
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}]}
severity_value(message_or_severity)
View Sourceseverity_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