You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@couchdb.apache.org by iilyak <gi...@git.apache.org> on 2015/11/18 17:45:20 UTC

[GitHub] couchdb-chttpd pull request: Handle errors from before_request/aft...

GitHub user iilyak opened a pull request:

    https://github.com/apache/couchdb-chttpd/pull/95

    Handle errors from before_request/after_request

    This refactoring fixes the handling of errors from before_request.
    Unify how we deal with errors among before_request and handle_request.

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/cloudant/couchdb-chttpd support-raise-from-before-requests

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/couchdb-chttpd/pull/95.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #95
    
----
commit 6a1858f8b2604c4af96b7ca4d423ebacd2f98603
Author: ILYA Khlopotov <ii...@ca.ibm.com>
Date:   2015-11-18T16:25:39Z

    Handle errors from before_request/after_request
    
    This refactoring fixes the handling of errors from before_request.
    Unify how we deal with errors among before_request and handle_request.

----


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] couchdb-chttpd pull request: Handle errors from before_request/aft...

Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:

    https://github.com/apache/couchdb-chttpd/pull/95


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] couchdb-chttpd pull request: Handle errors from before_request/aft...

Posted by kxepal <gi...@git.apache.org>.
Github user kxepal commented on the pull request:

    https://github.com/apache/couchdb-chttpd/pull/95#issuecomment-159392693
  
    Nice! +1


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] couchdb-chttpd pull request: Handle errors from before_request/aft...

Posted by kxepal <gi...@git.apache.org>.
Github user kxepal commented on the pull request:

    https://github.com/apache/couchdb-chttpd/pull/95#issuecomment-157782803
  
    Looks good for me, need to check however.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] couchdb-chttpd pull request: Handle errors from before_request/aft...

Posted by kxepal <gi...@git.apache.org>.
Github user kxepal commented on a diff in the pull request:

    https://github.com/apache/couchdb-chttpd/pull/95#discussion_r45228610
  
    --- Diff: src/chttpd.erl ---
    @@ -209,21 +204,58 @@ handle_request_int(MochiReq) ->
                     || Part <- string:tokens(Path, "/")]
         },
     
    -    {ok, HttpReq} = chttpd_plugin:before_request(HttpReq0),
    +    % put small token on heap to keep requests synced to backend calls
    +    erlang:put(nonce, Nonce),
    +
    +    % suppress duplicate log
    +    erlang:put(dont_log_request, true),
    +
    +    Result0 = case before_request(HttpReq0) of
    +        {ok, HttpReq1} ->
    +            process_request(HttpReq1);
    +        {error, Response} ->
    +            {HttpReq0, Response}
    +    end,
    +
    +    {HttpReq3, HttpResp0} = result(Result0, HttpReq0),
    --- End diff --
    
    The way how we form Result0 and how result/2 works very confusing. Especially for the case when before_request returns {error, Response}, because Response have to be {ok, #httpd{}}  or {aborted, MochiReq, Reason} or you'll get function_clause.
    
    Is it possible to make it more friendly for this refactor?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] couchdb-chttpd pull request: Handle errors from before_request/aft...

Posted by kxepal <gi...@git.apache.org>.
Github user kxepal commented on a diff in the pull request:

    https://github.com/apache/couchdb-chttpd/pull/95#discussion_r45225943
  
    --- Diff: src/chttpd.erl ---
    @@ -242,49 +274,47 @@ handle_request_int(MochiReq) ->
             Response ->
                 Response
             end
    -    catch
    -        throw:{http_head_abort, Resp0} ->
    -            {ok, Resp0};
    -        throw:{http_abort, Resp0, Reason0} ->
    -            {aborted, Resp0, Reason0};
    -        throw:{invalid_json, _} ->
    -            send_error(HttpReq, {bad_request, "invalid UTF-8 JSON"});
    -        exit:{mochiweb_recv_error, E} ->
    -            couch_log:notice(LogForClosedSocket ++ " - ~p", [E]),
    -            exit(normal);
    -        exit:{uri_too_long, _} ->
    -            send_error(HttpReq, request_uri_too_long);
    -        exit:{body_too_large, _} ->
    -            send_error(HttpReq, request_entity_too_large);
    -        throw:Error ->
    -            send_error(HttpReq, Error);
    -        error:database_does_not_exist ->
    -            send_error(HttpReq, database_does_not_exist);
    -        Tag:Error ->
    -            Stack = erlang:get_stacktrace(),
    -            % TODO improve logging and metrics collection for client disconnects
    -            case {Tag, Error, Stack} of
    -                {exit, normal, [{mochiweb_request, send, _, _} | _]} ->
    -                    exit(normal); % Client disconnect (R15+)
    -                {exit, normal, [{mochiweb_request, send, _} | _]} ->
    -                    exit(normal); % Client disconnect (R14)
    -                _Else ->
    -                    send_error(HttpReq, {Error, nil, Stack})
    -            end
    -    end,
    -
    -    {HttpReq1, HttpResp0} = result(Result0, HttpReq),
    -    {ok, HttpResp1} = chttpd_plugin:after_request(HttpReq1, HttpResp0),
    -
    -    HttpResp2 = update_stats(HttpReq1, HttpResp1),
    -    maybe_log(HttpReq1, HttpResp2),
    +    catch Tag:Error ->
    +        catch_error(HttpReq, Tag, Error)
    +    end.
     
    -    case HttpResp2 of
    -        #httpd_resp{status = ok, response = Resp} ->
    -            {ok, Resp};
    -        #httpd_resp{status = aborted, reason = Reason} ->
    -            couch_log:error("Response abnormally terminated: ~p", [Reason]),
    -            exit(normal)
    +catch_error(_HttpReq, throw, {http_head_abort, Resp}) ->
    +    {ok, Resp};
    +catch_error(_HttpReq, throw, {http_abort, Resp, Reason}) ->
    +    {aborted, Resp, Reason};
    +catch_error(HttpReq, throw, {invalid_json, _}) ->
    +    send_error(HttpReq, {bad_request, "invalid UTF-8 JSON"});
    +catch_error(HttpReq, exit, {mochiweb_recv_error, E}) ->
    +    #httpd{
    +        mochi_req = MochiReq,
    +        peer = Peer,
    +        original_method = Method
    +    } = HttpReq,
    +    LogForClosedSocket = io_lib:format("mochiweb_recv_error for ~s - ~p ~s", [
    +        Peer,
    +        Method,
    +        MochiReq:get(raw_path)
    +    ]),
    +    couch_log:notice(LogForClosedSocket ++ " - ~p", [E]),
    --- End diff --
    
    Why not to do formatting right in couch_log:notice call?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] couchdb-chttpd pull request: Handle errors from before_request/aft...

Posted by kxepal <gi...@git.apache.org>.
Github user kxepal commented on a diff in the pull request:

    https://github.com/apache/couchdb-chttpd/pull/95#discussion_r45225874
  
    --- Diff: src/chttpd.erl ---
    @@ -242,49 +274,47 @@ handle_request_int(MochiReq) ->
             Response ->
                 Response
             end
    -    catch
    -        throw:{http_head_abort, Resp0} ->
    -            {ok, Resp0};
    -        throw:{http_abort, Resp0, Reason0} ->
    -            {aborted, Resp0, Reason0};
    -        throw:{invalid_json, _} ->
    -            send_error(HttpReq, {bad_request, "invalid UTF-8 JSON"});
    -        exit:{mochiweb_recv_error, E} ->
    -            couch_log:notice(LogForClosedSocket ++ " - ~p", [E]),
    -            exit(normal);
    -        exit:{uri_too_long, _} ->
    -            send_error(HttpReq, request_uri_too_long);
    -        exit:{body_too_large, _} ->
    -            send_error(HttpReq, request_entity_too_large);
    -        throw:Error ->
    -            send_error(HttpReq, Error);
    -        error:database_does_not_exist ->
    -            send_error(HttpReq, database_does_not_exist);
    -        Tag:Error ->
    -            Stack = erlang:get_stacktrace(),
    -            % TODO improve logging and metrics collection for client disconnects
    -            case {Tag, Error, Stack} of
    -                {exit, normal, [{mochiweb_request, send, _, _} | _]} ->
    -                    exit(normal); % Client disconnect (R15+)
    -                {exit, normal, [{mochiweb_request, send, _} | _]} ->
    -                    exit(normal); % Client disconnect (R14)
    -                _Else ->
    -                    send_error(HttpReq, {Error, nil, Stack})
    -            end
    -    end,
    -
    -    {HttpReq1, HttpResp0} = result(Result0, HttpReq),
    -    {ok, HttpResp1} = chttpd_plugin:after_request(HttpReq1, HttpResp0),
    -
    -    HttpResp2 = update_stats(HttpReq1, HttpResp1),
    -    maybe_log(HttpReq1, HttpResp2),
    +    catch Tag:Error ->
    +        catch_error(HttpReq, Tag, Error)
    +    end.
     
    -    case HttpResp2 of
    -        #httpd_resp{status = ok, response = Resp} ->
    -            {ok, Resp};
    -        #httpd_resp{status = aborted, reason = Reason} ->
    -            couch_log:error("Response abnormally terminated: ~p", [Reason]),
    -            exit(normal)
    +catch_error(_HttpReq, throw, {http_head_abort, Resp}) ->
    +    {ok, Resp};
    +catch_error(_HttpReq, throw, {http_abort, Resp, Reason}) ->
    +    {aborted, Resp, Reason};
    +catch_error(HttpReq, throw, {invalid_json, _}) ->
    +    send_error(HttpReq, {bad_request, "invalid UTF-8 JSON"});
    +catch_error(HttpReq, exit, {mochiweb_recv_error, E}) ->
    +    #httpd{
    +        mochi_req = MochiReq,
    +        peer = Peer,
    +        original_method = Method
    +    } = HttpReq,
    +    LogForClosedSocket = io_lib:format("mochiweb_recv_error for ~s - ~p ~s", [
    +        Peer,
    +        Method,
    +        MochiReq:get(raw_path)
    +    ]),
    +    couch_log:notice(LogForClosedSocket ++ " - ~p", [E]),
    +    exit(normal);
    +catch_error(HttpReq, exit, {uri_too_long, _}) ->
    +    send_error(HttpReq, request_uri_too_long);
    +catch_error(HttpReq, exit, {body_too_large, _}) ->
    +    send_error(HttpReq, request_entity_too_large);
    +catch_error(HttpReq, throw, Error) ->
    +    send_error(HttpReq, Error);
    +catch_error(HttpReq, error, database_does_not_exist) ->
    +    send_error(HttpReq, database_does_not_exist);
    +catch_error(HttpReq, Tag, Error) ->
    +    Stack = erlang:get_stacktrace(),
    +    % TODO improve logging and metrics collection for client disconnects
    +    case {Tag, Error, Stack} of
    +        {exit, normal, [{mochiweb_request, send, _, _} | _]} ->
    +            exit(normal); % Client disconnect (R15+)
    +        {exit, normal, [{mochiweb_request, send, _} | _]} ->
    +            exit(normal); % Client disconnect (R14)
    --- End diff --
    
    You can drop this clause with the light on heart like we dropped R14 support.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---