Skip to content

Instantly share code, notes, and snippets.

@brendanhay
Created March 17, 2012 13:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brendanhay/2058721 to your computer and use it in GitHub Desktop.
Save brendanhay/2058721 to your computer and use it in GitHub Desktop.
AMQP single send - error monad vs nested case
-spec send(method(), #amqp_params_direct{}) -> error_m(ok, term()).
%% @private
send(Method, Params) ->
do([error_m ||
Conn <- amqp_connection:start(Params),
Chan <- amqp_connection:open_channel(Conn),
return(amqp_channel:call(Chan, Method)),
return(amqp_channel:close(Chan)),
amqp_connection:close(Conn)]).
-spec send(method(), #amqp_params_direct{}) -> ok.
%% @private
send(Method, Params) ->
{Conn, Chan} =
case amqp_connection:start(Params) of
{ok, Conn} ->
case amqp_connection:open_channel(Conn) of
{ok, Chan} ->
amqp_channel:call(Chan, Method),
{Conn, Chan};
Error ->
{Conn, none}
end;
Error ->
{none, none}
end,
case Chan of
none -> ok;
_ -> amqp_channel:close(Chan)
end,
case Conn of
none -> ok;
_ -> amqp_connection:close(Conn)
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment