mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-14 20:30:23 +00:00
Add a cowboy_req:to_list/1 function
Removes a lot of duplicate code when printing error messages.
This commit is contained in:
parent
9b5cbb5000
commit
bb4bd9ac96
4 changed files with 14 additions and 8 deletions
|
@ -287,7 +287,7 @@ handler_init(Req, State=#state{transport=Transport,
|
|||
upgrade_protocol(Req, State, Module)
|
||||
catch Class:Reason ->
|
||||
error_terminate(500, State),
|
||||
PLReq = lists:zip(record_info(fields, http_req), tl(tuple_to_list(Req))),
|
||||
PLReq = cowboy_req:to_list(Req),
|
||||
error_logger:error_msg(
|
||||
"** Handler ~p terminating in init/3~n"
|
||||
" for the reason ~p:~p~n"
|
||||
|
@ -310,7 +310,7 @@ handler_handle(HandlerState, Req, State=#state{handler={Handler, Opts}}) ->
|
|||
{ok, Req2, HandlerState2} ->
|
||||
terminate_request(HandlerState2, Req2, State)
|
||||
catch Class:Reason ->
|
||||
PLReq = lists:zip(record_info(fields, http_req), tl(tuple_to_list(Req))),
|
||||
PLReq = cowboy_req:to_list(Req),
|
||||
error_logger:error_msg(
|
||||
"** Handler ~p terminating in handle/2~n"
|
||||
" for the reason ~p:~p~n"
|
||||
|
@ -368,7 +368,7 @@ handler_call(HandlerState, Req, State=#state{handler={Handler, Opts}},
|
|||
handler_before_loop(HandlerState2, Req2,
|
||||
State#state{hibernate=true})
|
||||
catch Class:Reason ->
|
||||
PLReq = lists:zip(record_info(fields, http_req), tl(tuple_to_list(Req))),
|
||||
PLReq = cowboy_req:to_list(Req),
|
||||
error_logger:error_msg(
|
||||
"** Handler ~p terminating in info/3~n"
|
||||
" for the reason ~p:~p~n"
|
||||
|
@ -385,7 +385,7 @@ handler_terminate(HandlerState, Req, #state{handler={Handler, Opts}}) ->
|
|||
try
|
||||
Handler:terminate(Req#http_req{resp_state=locked}, HandlerState)
|
||||
catch Class:Reason ->
|
||||
PLReq = lists:zip(record_info(fields, http_req), tl(tuple_to_list(Req))),
|
||||
PLReq = cowboy_req:to_list(Req),
|
||||
error_logger:error_msg(
|
||||
"** Handler ~p terminating in terminate/2~n"
|
||||
" for the reason ~p:~p~n"
|
||||
|
|
|
@ -78,6 +78,7 @@
|
|||
|
||||
%% Misc API.
|
||||
-export([compact/1]).
|
||||
-export([to_list/1]).
|
||||
-export([transport/1]).
|
||||
|
||||
-include("http.hrl").
|
||||
|
@ -816,6 +817,11 @@ compact(Req) ->
|
|||
bindings=undefined, headers=[],
|
||||
p_headers=[], cookies=[]}.
|
||||
|
||||
%% @doc Convert the Req object to a list of key/values.
|
||||
-spec to_list(req()) -> [{atom(), any()}].
|
||||
to_list(Req) ->
|
||||
lists:zip(record_info(fields, http_req), tl(tuple_to_list(Req))).
|
||||
|
||||
%% @doc Return the transport module and socket associated with a request.
|
||||
%%
|
||||
%% This exposes the same socket interface used internally by the HTTP protocol
|
||||
|
|
|
@ -69,7 +69,7 @@ upgrade(_ListenerPid, Handler, Opts, Req) ->
|
|||
service_available(Req, #state{handler=Handler})
|
||||
end
|
||||
catch Class:Reason ->
|
||||
PLReq = lists:zip(record_info(fields, http_req), tl(tuple_to_list(Req))),
|
||||
PLReq = cowboy_req:to_list(Req),
|
||||
error_logger:error_msg(
|
||||
"** Handler ~p terminating in rest_init/3~n"
|
||||
" for the reason ~p:~p~n** Options were ~p~n"
|
||||
|
|
|
@ -125,7 +125,7 @@ handler_init(State=#state{handler=Handler, opts=Opts},
|
|||
upgrade_denied(Req2)
|
||||
catch Class:Reason ->
|
||||
upgrade_error(Req),
|
||||
PLReq = lists:zip(record_info(fields, http_req), tl(tuple_to_list(Req))),
|
||||
PLReq = cowboy_req:to_list(Req),
|
||||
error_logger:error_msg(
|
||||
"** Handler ~p terminating in websocket_init/3~n"
|
||||
" for the reason ~p:~p~n** Options were ~p~n"
|
||||
|
@ -473,7 +473,7 @@ handler_call(State=#state{handler=Handler, opts=Opts}, Req, HandlerState,
|
|||
{shutdown, Req2, HandlerState2} ->
|
||||
websocket_close(State, Req2, HandlerState2, {normal, shutdown})
|
||||
catch Class:Reason ->
|
||||
PLReq = lists:zip(record_info(fields, http_req), tl(tuple_to_list(Req))),
|
||||
PLReq = cowboy_req:to_list(Req),
|
||||
error_logger:error_msg(
|
||||
"** Handler ~p terminating in ~p/3~n"
|
||||
" for the reason ~p:~p~n** Message was ~p~n"
|
||||
|
@ -523,7 +523,7 @@ handler_terminate(#state{handler=Handler, opts=Opts},
|
|||
try
|
||||
Handler:websocket_terminate(TerminateReason, Req, HandlerState)
|
||||
catch Class:Reason ->
|
||||
PLReq = lists:zip(record_info(fields, http_req), tl(tuple_to_list(Req))),
|
||||
PLReq = cowboy_req:to_list(Req),
|
||||
error_logger:error_msg(
|
||||
"** Handler ~p terminating in websocket_terminate/3~n"
|
||||
" for the reason ~p:~p~n** Initial reason was ~p~n"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue