Created
September 30, 2016 09:49
-
-
Save anonymous/7e4b85fbf6c0e208af718e8ce08a694d to your computer and use it in GitHub Desktop.
Standard Error Format for Erlang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-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