diff options
author | Loïc Hoguin <[email protected]> | 2012-09-15 20:33:57 +0200 |
---|---|---|
committer | Loïc Hoguin <[email protected]> | 2012-09-15 20:33:57 +0200 |
commit | bb4bd9ac96d5fde5e8183c9950d3642062347a45 (patch) | |
tree | 1ca6ecc5575355c6c2f197ab3b53f1f90f0b2809 | |
parent | 9b5cbb5000e564352afbd762c4ab0406250f147f (diff) | |
download | cowboy-bb4bd9ac96d5fde5e8183c9950d3642062347a45.tar.gz cowboy-bb4bd9ac96d5fde5e8183c9950d3642062347a45.tar.bz2 cowboy-bb4bd9ac96d5fde5e8183c9950d3642062347a45.zip |
Add a cowboy_req:to_list/1 function
Removes a lot of duplicate code when printing error messages.
-rw-r--r-- | src/cowboy_protocol.erl | 8 | ||||
-rw-r--r-- | src/cowboy_req.erl | 6 | ||||
-rw-r--r-- | src/cowboy_rest.erl | 2 | ||||
-rw-r--r-- | src/cowboy_websocket.erl | 6 |
4 files changed, 14 insertions, 8 deletions
diff --git a/src/cowboy_protocol.erl b/src/cowboy_protocol.erl index 089cbbc..bc56e76 100644 --- a/src/cowboy_protocol.erl +++ b/src/cowboy_protocol.erl @@ -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" diff --git a/src/cowboy_req.erl b/src/cowboy_req.erl index e0804a6..c7bb64b 100644 --- a/src/cowboy_req.erl +++ b/src/cowboy_req.erl @@ -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 diff --git a/src/cowboy_rest.erl b/src/cowboy_rest.erl index 36a97f3..fc4279b 100644 --- a/src/cowboy_rest.erl +++ b/src/cowboy_rest.erl @@ -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" diff --git a/src/cowboy_websocket.erl b/src/cowboy_websocket.erl index 8d40b73..182f80d 100644 --- a/src/cowboy_websocket.erl +++ b/src/cowboy_websocket.erl @@ -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" |