mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-15 12:40:25 +00:00
Remove the error tuple return value for middlewares
It wasn't interesting compared to simply returning a halt tuple with an explicit reply.
This commit is contained in:
parent
aa4d86b81f
commit
c56bada509
8 changed files with 8 additions and 31 deletions
|
@ -38,11 +38,6 @@ callbacks will be removed in favor of a unified `terminate/3`.
|
||||||
|
|
||||||
The `terminate/3` callback will become optional.
|
The `terminate/3` callback will become optional.
|
||||||
|
|
||||||
### Middlewares
|
|
||||||
|
|
||||||
The error tuple return value brings little value compared to
|
|
||||||
the halt tuple. The error tuple will therefore be removed.
|
|
||||||
|
|
||||||
### Hooks
|
### Hooks
|
||||||
|
|
||||||
The interface of the `onresponse` hook will change. There has
|
The interface of the `onresponse` hook will change. There has
|
||||||
|
|
|
@ -24,7 +24,6 @@ Middlewares can return one of four different values:
|
||||||
* `{ok, Req, Env}` to continue the request processing
|
* `{ok, Req, Env}` to continue the request processing
|
||||||
* `{suspend, Module, Function, Args}` to hibernate
|
* `{suspend, Module, Function, Args}` to hibernate
|
||||||
* `{halt, Req}` to stop processing and move on to the next request
|
* `{halt, Req}` to stop processing and move on to the next request
|
||||||
* `{error, StatusCode, Req}` to reply an error and close the socket
|
|
||||||
|
|
||||||
Of note is that when hibernating, processing will resume on the given
|
Of note is that when hibernating, processing will resume on the given
|
||||||
MFA, discarding all previous stacktrace. Make sure you keep the `Req`
|
MFA, discarding all previous stacktrace. Make sure you keep the `Req`
|
||||||
|
|
|
@ -22,7 +22,6 @@ optionally with its contents modified.
|
||||||
-> {ok, Req, Env}
|
-> {ok, Req, Env}
|
||||||
| {suspend, Module, Function, Args}
|
| {suspend, Module, Function, Args}
|
||||||
| {halt, Req}
|
| {halt, Req}
|
||||||
| {error, StatusCode, Req}
|
|
||||||
|
|
||||||
Types:
|
Types:
|
||||||
|
|
||||||
|
@ -31,7 +30,6 @@ Types:
|
||||||
* Module = module()
|
* Module = module()
|
||||||
* Function = atom()
|
* Function = atom()
|
||||||
* Args = [any()]
|
* Args = [any()]
|
||||||
* StatusCode = cowboy:http_status()
|
|
||||||
|
|
||||||
Execute the middleware.
|
Execute the middleware.
|
||||||
|
|
||||||
|
@ -47,8 +45,3 @@ The `halt` return value stops Cowboy from doing any further
|
||||||
processing of the request, even if there are middlewares
|
processing of the request, even if there are middlewares
|
||||||
that haven't been executed yet. The connection may be left
|
that haven't been executed yet. The connection may be left
|
||||||
open to receive more requests from the client.
|
open to receive more requests from the client.
|
||||||
|
|
||||||
The `error` return value sends an error response identified
|
|
||||||
by the `StatusCode` and then proceeds to terminate the
|
|
||||||
connection. Middlewares that haven't been executed yet
|
|
||||||
will not be called.
|
|
||||||
|
|
|
@ -99,7 +99,6 @@ handler_init(Req, State, Handler, HandlerOpts) ->
|
||||||
-> {ok, Req, Env}
|
-> {ok, Req, Env}
|
||||||
| {suspend, module(), atom(), any()}
|
| {suspend, module(), atom(), any()}
|
||||||
| {halt, Req}
|
| {halt, Req}
|
||||||
| {error, cowboy:http_status(), Req}
|
|
||||||
when Req::cowboy_req:req(), Env::cowboy_middleware:env().
|
when Req::cowboy_req:req(), Env::cowboy_middleware:env().
|
||||||
upgrade_protocol(Req, #state{env=Env},
|
upgrade_protocol(Req, #state{env=Env},
|
||||||
Handler, HandlerOpts, Module) ->
|
Handler, HandlerOpts, Module) ->
|
||||||
|
|
|
@ -21,5 +21,4 @@
|
||||||
-> {ok, Req, Env}
|
-> {ok, Req, Env}
|
||||||
| {suspend, module(), atom(), [any()]}
|
| {suspend, module(), atom(), [any()]}
|
||||||
| {halt, Req}
|
| {halt, Req}
|
||||||
| {error, cowboy:http_status(), Req}
|
|
||||||
when Req::cowboy_req:req(), Env::env().
|
when Req::cowboy_req:req(), Env::env().
|
||||||
|
|
|
@ -428,9 +428,7 @@ execute(Req, State, Env, [Middleware|Tail]) ->
|
||||||
erlang:hibernate(?MODULE, resume,
|
erlang:hibernate(?MODULE, resume,
|
||||||
[State, Env, Tail, Module, Function, Args]);
|
[State, Env, Tail, Module, Function, Args]);
|
||||||
{halt, Req2} ->
|
{halt, Req2} ->
|
||||||
next_request(Req2, State, ok);
|
next_request(Req2, State, ok)
|
||||||
{error, Code, Req2} ->
|
|
||||||
error_terminate(Code, Req2, State)
|
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec resume(#state{}, cowboy_middleware:env(), [module()],
|
-spec resume(#state{}, cowboy_middleware:env(), [module()],
|
||||||
|
@ -443,9 +441,7 @@ resume(State, Env, Tail, Module, Function, Args) ->
|
||||||
erlang:hibernate(?MODULE, resume,
|
erlang:hibernate(?MODULE, resume,
|
||||||
[State, Env, Tail, Module2, Function2, Args2]);
|
[State, Env, Tail, Module2, Function2, Args2]);
|
||||||
{halt, Req2} ->
|
{halt, Req2} ->
|
||||||
next_request(Req2, State, ok);
|
next_request(Req2, State, ok)
|
||||||
{error, Code, Req2} ->
|
|
||||||
error_terminate(Code, Req2, State)
|
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec next_request(cowboy_req:req(), #state{}, any()) -> ok.
|
-spec next_request(cowboy_req:req(), #state{}, any()) -> ok.
|
||||||
|
|
|
@ -157,7 +157,7 @@ compile_brackets_split(<< C, Rest/binary >>, Acc, N) ->
|
||||||
compile_brackets_split(Rest, << Acc/binary, C >>, N).
|
compile_brackets_split(Rest, << Acc/binary, C >>, N).
|
||||||
|
|
||||||
-spec execute(Req, Env)
|
-spec execute(Req, Env)
|
||||||
-> {ok, Req, Env} | {error, 400 | 404, Req}
|
-> {ok, Req, Env} | {halt, Req}
|
||||||
when Req::cowboy_req:req(), Env::cowboy_middleware:env().
|
when Req::cowboy_req:req(), Env::cowboy_middleware:env().
|
||||||
execute(Req, Env) ->
|
execute(Req, Env) ->
|
||||||
{_, Dispatch} = lists:keyfind(dispatch, 1, Env),
|
{_, Dispatch} = lists:keyfind(dispatch, 1, Env),
|
||||||
|
@ -168,11 +168,11 @@ execute(Req, Env) ->
|
||||||
Req2 = cowboy_req:set_bindings(HostInfo, PathInfo, Bindings, Req),
|
Req2 = cowboy_req:set_bindings(HostInfo, PathInfo, Bindings, Req),
|
||||||
{ok, Req2, [{handler, Handler}, {handler_opts, HandlerOpts}|Env]};
|
{ok, Req2, [{handler, Handler}, {handler_opts, HandlerOpts}|Env]};
|
||||||
{error, notfound, host} ->
|
{error, notfound, host} ->
|
||||||
{error, 400, Req};
|
{halt, cowboy_req:reply(400, Req)};
|
||||||
{error, badrequest, path} ->
|
{error, badrequest, path} ->
|
||||||
{error, 400, Req};
|
{halt, cowboy_req:reply(400, Req)};
|
||||||
{error, notfound, path} ->
|
{error, notfound, path} ->
|
||||||
{error, 404, Req}
|
{halt, cowboy_req:reply(404, Req)}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
%% Internal.
|
%% Internal.
|
||||||
|
|
|
@ -407,9 +407,7 @@ execute(Req, Env, [Middleware|Tail]) ->
|
||||||
erlang:hibernate(?MODULE, resume,
|
erlang:hibernate(?MODULE, resume,
|
||||||
[Env, Tail, Module, Function, Args]);
|
[Env, Tail, Module, Function, Args]);
|
||||||
{halt, Req2} ->
|
{halt, Req2} ->
|
||||||
cowboy_req:ensure_response(Req2, 204);
|
cowboy_req:ensure_response(Req2, 204)
|
||||||
{error, Status, Req2} ->
|
|
||||||
cowboy_req:reply(Status, Req2)
|
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec resume(cowboy_middleware:env(), [module()],
|
-spec resume(cowboy_middleware:env(), [module()],
|
||||||
|
@ -422,9 +420,7 @@ resume(Env, Tail, Module, Function, Args) ->
|
||||||
erlang:hibernate(?MODULE, resume,
|
erlang:hibernate(?MODULE, resume,
|
||||||
[Env, Tail, Module2, Function2, Args2]);
|
[Env, Tail, Module2, Function2, Args2]);
|
||||||
{halt, Req2} ->
|
{halt, Req2} ->
|
||||||
cowboy_req:ensure_response(Req2, 204);
|
cowboy_req:ensure_response(Req2, 204)
|
||||||
{error, Status, Req2} ->
|
|
||||||
cowboy_req:reply(Status, Req2)
|
|
||||||
end.
|
end.
|
||||||
|
|
||||||
%% Reply functions used by cowboy_req.
|
%% Reply functions used by cowboy_req.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue