Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Simpler error handling for lhttpc users #10

Merged
merged 1 commit into from

2 participants

Filipe Manana Michal Ptaszek
Filipe Manana

On error we now always get a response of the form
{error, Reason} instead of either getting such a
response or an exit (which is undocumented).
This simplifies code which wants to deal with all
possible errors, for example:

try
    case lhttpc:send_body_part(UploadState, BodyPart, Timeout) of
    {ok, UploadState2} ->
        loop(UpfateState2, Timeout);
    {error, Reason} ->
        handle_error(Reason)
     end
 catch exit:ExitReason ->
     handle_error(ExitReason)
 end

Would become simply:

case lhttpc:send_body_part(UploadState, BodyPart, Timeout) of
{ok, UploadState2} ->
    loop(UpfateState2, Timeout);
{error, Reason} ->
    handle_error(Reason)
end
Filipe Manana fdmanana Simpler error handling
On error we now always get a response of the form
{error, Reason} instead of either getting such a
response or an exit (which is undocumented).
This simplifies code which wants to deal with all
possible errors, for example:

    try
        case lhttpc:send_body_part(UploadState, BodyPart, Timeout) of
        {ok, UploadState2} ->
            loop(UpfateState2, Timeout);
        {error, Reason} ->
            handle_error(Reason)
         end
     catch exit:ExitReason ->
         handle_error(ExitReason)
     end

Would become simply:

    case lhttpc:send_body_part(UploadState, BodyPart, Timeout) of
    {ok, UploadState2} ->
        loop(UpfateState2, Timeout);
    {error, Reason} ->
        handle_error(Reason)
    end
4afde00
Michal Ptaszek paulgray merged commit 595f35c into from
Sarath Lakshman t3rm1n4l referenced this pull request from a commit in t3rm1n4l/couchdb
Filipe Manana fdmanana Update lhttpc to latest version
This simplifies error handling and fixes many
dialyzer warnings. Related upstream pull requests
are:

* esl/lhttpc#10
* esl/lhttpc#11
* esl/lhttpc#12

This is part of CBD-73

Change-Id: I5c7239ee67cbb1fa5b88fd51e9ce803e9b244f6a
Reviewed-on: http://review.couchbase.org/15099
Tested-by: buildbot <build@couchbase.com>
Reviewed-by: Volker Mische <volker.mische@gmail.com>
eeff685
Yakov egobrain referenced this pull request from a commit in egobrain/lhttpc
Filipe Manana fdmanana Simpler and consistent error handling
Don't raise an error if the http client process died
with a reason other than 'timeout'. Instead return a
term with the shape {error, any()}.
This was missed in the following pull request:

esl#10
5101d8a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 5, 2012
  1. Filipe Manana

    Simpler error handling

    fdmanana authored
    On error we now always get a response of the form
    {error, Reason} instead of either getting such a
    response or an exit (which is undocumented).
    This simplifies code which wants to deal with all
    possible errors, for example:
    
        try
            case lhttpc:send_body_part(UploadState, BodyPart, Timeout) of
            {ok, UploadState2} ->
                loop(UpfateState2, Timeout);
            {error, Reason} ->
                handle_error(Reason)
             end
         catch exit:ExitReason ->
             handle_error(ExitReason)
         end
    
    Would become simply:
    
        case lhttpc:send_body_part(UploadState, BodyPart, Timeout) of
        {ok, UploadState2} ->
            loop(UpfateState2, Timeout);
        {error, Reason} ->
            handle_error(Reason)
        end
This page is out of date. Refresh to see the latest.
Showing with 8 additions and 18 deletions.
  1. +5 −16 src/lhttpc.erl
  2. +3 −2 src/lhttpc_client.erl
21 src/lhttpc.erl
View
@@ -352,16 +352,11 @@ request(Host, Port, Ssl, Path, Method, Hdrs, Body, Timeout, Options) ->
receive
{response, Pid, R} ->
R;
- {exit, Pid, Reason} ->
- % We would rather want to exit here, instead of letting the
- % linked client send us an exit signal, since this can be
- % caught by the caller.
- exit(Reason);
{'EXIT', Pid, Reason} ->
- % This could happen if the process we're running in taps exits
+ % This could happen if the process we're running in traps exits
% and the client process exits due to some exit signal being
% sent to it. Very unlikely though
- exit(Reason)
+ {error, Reason}
after Timeout ->
kill_client(Pid)
end.
@@ -415,10 +410,8 @@ send_body_part({Pid, 0}, IoList, Timeout) when is_pid(Pid) ->
send_body_part({Pid, 1}, IoList, Timeout);
{response, Pid, R} ->
R;
- {exit, Pid, Reason} ->
- exit(Reason);
{'EXIT', Pid, Reason} ->
- exit(Reason)
+ {error, Reason}
after Timeout ->
kill_client(Pid)
end;
@@ -430,10 +423,8 @@ send_body_part({Pid, Window}, IoList, _Timeout) when Window > 0, is_pid(Pid) ->
{ok, {Pid, Window}};
{response, Pid, R} ->
R;
- {exit, Pid, Reason} ->
- exit(Reason);
{'EXIT', Pid, Reason} ->
- exit(Reason)
+ {error, Reason}
after 0 ->
{ok, {Pid, lhttpc_lib:dec(Window)}}
end.
@@ -533,10 +524,8 @@ read_response(Pid, Timeout) ->
read_response(Pid, Timeout);
{response, Pid, R} ->
R;
- {exit, Pid, Reason} ->
- exit(Reason);
{'EXIT', Pid, Reason} ->
- exit(Reason)
+ {error, Reason}
after Timeout ->
kill_client(Pid)
end.
5 src/lhttpc_client.erl
View
@@ -86,8 +86,9 @@ request(From, Host, Port, Ssl, Path, Method, Hdrs, Body, Options) ->
{response, self(), {error, Reason}};
error:closed ->
{response, self(), {error, connection_closed}};
- error:Error ->
- {exit, self(), {Error, erlang:get_stacktrace()}}
+ error:Reason ->
+ Stack = erlang:get_stacktrace(),
+ {response, self(), {error, {Reason, Stack}}}
end,
case Result of
{response, _, {ok, {no_return, _}}} -> ok;
Something went wrong with that request. Please try again.