Use this module to create your error aggregator and handler.
For example:
defmodule MyApp.Errors do
use Splode, error_classes: [
invalid: MyApp.Errors.Invalid,
unknown: MyApp.Errors.Unknown
],
unknown_error: MyApp.Errors.Unknown.Unknown
endOptions
:error_classes- A keyword list mapping error class atoms to error class modules. At least one error class must be provided.:unknown_error- The module to use when an error cannot be converted to a known type. This is required.:merge_with- A list of other Splode modules whose errors should be recognized and flattened when combined. Optional.:filter_stacktraces- A list of modules or module prefixes to filter from stacktraces. For each consecutive sequence of frames matching any filter, only the deepest (last) frame is kept. This is useful for hiding internal implementation details from error stacktraces. Accepts atoms (exact module match) or strings (prefix match). Optional.Elixir standard library frames (Enum, Stream, List, Map, etc.) are treated as part of an active matching sequence but are not kept as the "deepest" frame. This prevents stdlib frames from appearing in filtered stacktraces when they're sandwiched between internal module calls.
defmodule MyApp.Errors do use Splode, error_classes: [invalid: MyApp.Errors.Invalid], unknown_error: MyApp.Errors.Unknown, filter_stacktraces: [MyApp.Internal, "MyApp.Internal."] end
Summary
Callbacks
Converts a combination of a module and json input into an Splode exception.
Sets the path on the error or errors
Returns true if the given value is a splode error.
Combine errors into an error class
Turns any value into a splode error
Traverses errors, calling fun for each leaf error, and returns a nested map
of results grouped by each error's path.
Functions
Traverses errors, calling fun for each leaf error, and returns a nested map
of results grouped by each error's path.
Callbacks
@callback from_json(module(), map()) :: Splode.Error.t()
Converts a combination of a module and json input into an Splode exception.
This allows for errors to be serialized and deserialized
@callback set_path(Splode.Error.t() | [Splode.Error.t()], term() | [term()]) :: Splode.Error.t() | [Splode.Error.t()]
Sets the path on the error or errors
Returns true if the given value is a splode error.
@callback to_class(any()) :: Splode.Error.t()
Combine errors into an error class
@callback to_error(any()) :: Splode.Error.t()
Turns any value into a splode error
@callback traverse_errors( Splode.Error.t() | [Splode.Error.t()], (Splode.Error.t() -> term()) ) :: map()
Traverses errors, calling fun for each leaf error, and returns a nested map
of results grouped by each error's path.
This is useful for turning a Splode error into a simple, queryable structure
for testing or display purposes. The fun receives each individual (leaf)
error struct and should return a value (typically a formatted string).
Example
iex> traverse_errors(error, fn error -> Exception.message(error) end)
%{name: ["name is required"], email: ["email is invalid"]}Errors with nested paths produce nested maps:
iex> traverse_errors(error, &Exception.message/1)
%{user: %{email: ["email is invalid"]}}Errors with an empty path (i.e., global/root-level errors) are grouped under
the [] key.
Functions
Traverses errors, calling fun for each leaf error, and returns a nested map
of results grouped by each error's path.
Handles error class structs (which contain nested errors lists) by
recursively descending into them. Leaf errors are passed to fun and their
return values are collected into a list at the appropriate path key.
Errors with an empty path are grouped under the [] key.
Example
Splode.traverse_errors(error, fn error -> Exception.message(error) end)
#=> %{name: ["name is required"], email: ["email is invalid"]}