Haskell do syntax and Error monad extension for Erlang. Implemented as Parse Transform.
- add to deps in rebar.conf:
{erroico, ".*", {git, "https://github.com/Tpukep/erroico", "master"}}
- Include erroico definitions in your module:
-include_lib("erroico/include/erroico.hrl").
-module(erroico_demo).
-include_lib("erroico/include/erroico.hrl").
-export([info_update/2]).
fetch_info(UserId, Token) ->
%% try to fetch from some data source and fail
{error, "Fetch error"}.
%% {ok, "Some info"}.
modify_info(Info) ->
%% some processing here
{ok, Info}.
save_info(UserId, NewInfo) ->
%% save new info
{ok, {UserId, saved}}.
info_update(UserId, Token) ->
do(begin
Info = fetch_info(UserId, Token),
NewInfo = modify_info(Info),
save_info(UserId, NewInfo)
end).
1> erroico_demo:info_update("demoUser", "token").
{error, "Fetch error"}
When error result({error, Reason}) is returned by some function in do block rest functions are ignored and error result is returned. If no error occured then all functions will be executed in the block and success result({ok, Result}) will be returned. Try comment error result and uncomment success result in fetch_info function.
2> erroico_demo:info_update("demoUser", "token").
{ok,{"demoUser",saved}}