Skip to content

Instantly share code, notes, and snippets.

Created September 30, 2016 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/7e4b85fbf6c0e208af718e8ce08a694d to your computer and use it in GitHub Desktop.
Save anonymous/7e4b85fbf6c0e208af718e8ce08a694d to your computer and use it in GitHub Desktop.
Standard Error Format for Erlang
-record(error, {type, ctx}).
% Macros to create error adding module, line and function to context
% line(#error{}), module(#error{}), function(#error{}) to return those fields if defined or undefined if not
new(Type) -> new(Type, #{}).
new(Type, Ctx) -> #error{type=Type, ctx=Ctx}.
wrap(Type, Cause=#error{}) -> wrap(Type, Cause, Ctx).
wrap(Type, Cause=#error{}, Ctx) ->
#error{type=Type, ctx=maps:merge(Ctx, #{cause => Cause})}.
cause(#error{ctx=#{cause := Cause}}) -> {ok, Cause};
cause(#error{}) -> notfound.
get(Error=#error{}, Key) -> get(Error, Key, undefined).
get(Error=#error{ctx=Ctx}, Key, Default) -> maps:get(Key, Ctx, Default).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment