aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2011-07-07 17:23:53 +0200
committerLoïc Hoguin <[email protected]>2011-07-07 17:45:28 +0200
commit474f4e0bfab5a3dadc67e1b7f13c45b246e47433 (patch)
treeec7b6853b306f7ed32f82c588fce7ec5e604d909
parentaa0a66749e23fe8d29421794ce8ab173b062a311 (diff)
downloadcowboy-474f4e0bfab5a3dadc67e1b7f13c45b246e47433.tar.gz
cowboy-474f4e0bfab5a3dadc67e1b7f13c45b246e47433.tar.bz2
cowboy-474f4e0bfab5a3dadc67e1b7f13c45b246e47433.zip
Call Handler:terminate/2 even on error in Handler:handle/2
This ensures we can cleanup what we did in Handler:init/3.
-rw-r--r--src/cowboy_http_protocol.erl20
1 files changed, 12 insertions, 8 deletions
diff --git a/src/cowboy_http_protocol.erl b/src/cowboy_http_protocol.erl
index 837d837..421978b 100644
--- a/src/cowboy_http_protocol.erl
+++ b/src/cowboy_http_protocol.erl
@@ -209,21 +209,21 @@ handler_init(Req, State=#state{
handler_loop(HandlerState, Req, State=#state{handler={Handler, Opts}}) ->
try Handler:handle(Req#http_req{resp_state=waiting}, HandlerState) of
{ok, Req2, HandlerState2} ->
- handler_terminate(HandlerState2, Req2, State)
+ next_request(HandlerState2, Req2, State)
catch Class:Reason ->
- terminate(State),
error_logger:error_msg(
"** Handler ~p terminating in handle/2 for the reason ~p:~p~n"
"** Options were ~p~n** Handler state was ~p~n"
"** Request was ~p~n** Stacktrace: ~p~n~n",
[Handler, Class, Reason, Opts,
- HandlerState, Req, erlang:get_stacktrace()])
+ HandlerState, Req, erlang:get_stacktrace()]),
+ handler_terminate(HandlerState, Req, State),
+ terminate(State)
end.
--spec handler_terminate(any(), #http_req{}, #state{}) -> ok.
-handler_terminate(HandlerState, Req=#http_req{buffer=Buffer},
- State=#state{handler={Handler, Opts}}) ->
- HandlerRes = try
+-spec handler_terminate(any(), #http_req{}, #state{}) -> ok | error.
+handler_terminate(HandlerState, Req, #state{handler={Handler, Opts}}) ->
+ try
Handler:terminate(Req#http_req{resp_state=locked}, HandlerState)
catch Class:Reason ->
error_logger:error_msg(
@@ -233,7 +233,11 @@ handler_terminate(HandlerState, Req=#http_req{buffer=Buffer},
[Handler, Class, Reason, Opts,
HandlerState, Req, erlang:get_stacktrace()]),
error
- end,
+ end.
+
+-spec next_request(any(), #http_req{}, #state{}) -> ok.
+next_request(HandlerState, Req=#http_req{buffer=Buffer}, State) ->
+ HandlerRes = handler_terminate(HandlerState, Req, State),
BodyRes = ensure_body_processed(Req),
RespRes = ensure_response(Req, State),
case {HandlerRes, BodyRes, RespRes, State#state.connection} of