mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-14 20:30:23 +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.
|
||||
|
||||
### Middlewares
|
||||
|
||||
The error tuple return value brings little value compared to
|
||||
the halt tuple. The error tuple will therefore be removed.
|
||||
|
||||
### Hooks
|
||||
|
||||
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
|
||||
* `{suspend, Module, Function, Args}` to hibernate
|
||||
* `{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
|
||||
MFA, discarding all previous stacktrace. Make sure you keep the `Req`
|
||||
|
|
|
@ -22,7 +22,6 @@ optionally with its contents modified.
|
|||
-> {ok, Req, Env}
|
||||
| {suspend, Module, Function, Args}
|
||||
| {halt, Req}
|
||||
| {error, StatusCode, Req}
|
||||
|
||||
Types:
|
||||
|
||||
|
@ -31,7 +30,6 @@ Types:
|
|||
* Module = module()
|
||||
* Function = atom()
|
||||
* Args = [any()]
|
||||
* StatusCode = cowboy:http_status()
|
||||
|
||||
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
|
||||
that haven't been executed yet. The connection may be left
|
||||
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}
|
||||
| {suspend, module(), atom(), any()}
|
||||
| {halt, Req}
|
||||
| {error, cowboy:http_status(), Req}
|
||||
when Req::cowboy_req:req(), Env::cowboy_middleware:env().
|
||||
upgrade_protocol(Req, #state{env=Env},
|
||||
Handler, HandlerOpts, Module) ->
|
||||
|
|
|
@ -21,5 +21,4 @@
|
|||
-> {ok, Req, Env}
|
||||
| {suspend, module(), atom(), [any()]}
|
||||
| {halt, Req}
|
||||
| {error, cowboy:http_status(), Req}
|
||||
when Req::cowboy_req:req(), Env::env().
|
||||
|
|
|
@ -428,9 +428,7 @@ execute(Req, State, Env, [Middleware|Tail]) ->
|
|||
erlang:hibernate(?MODULE, resume,
|
||||
[State, Env, Tail, Module, Function, Args]);
|
||||
{halt, Req2} ->
|
||||
next_request(Req2, State, ok);
|
||||
{error, Code, Req2} ->
|
||||
error_terminate(Code, Req2, State)
|
||||
next_request(Req2, State, ok)
|
||||
end.
|
||||
|
||||
-spec resume(#state{}, cowboy_middleware:env(), [module()],
|
||||
|
@ -443,9 +441,7 @@ resume(State, Env, Tail, Module, Function, Args) ->
|
|||
erlang:hibernate(?MODULE, resume,
|
||||
[State, Env, Tail, Module2, Function2, Args2]);
|
||||
{halt, Req2} ->
|
||||
next_request(Req2, State, ok);
|
||||
{error, Code, Req2} ->
|
||||
error_terminate(Code, Req2, State)
|
||||
next_request(Req2, State, ok)
|
||||
end.
|
||||
|
||||
-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).
|
||||
|
||||
-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().
|
||||
execute(Req, Env) ->
|
||||
{_, Dispatch} = lists:keyfind(dispatch, 1, Env),
|
||||
|
@ -168,11 +168,11 @@ execute(Req, Env) ->
|
|||
Req2 = cowboy_req:set_bindings(HostInfo, PathInfo, Bindings, Req),
|
||||
{ok, Req2, [{handler, Handler}, {handler_opts, HandlerOpts}|Env]};
|
||||
{error, notfound, host} ->
|
||||
{error, 400, Req};
|
||||
{halt, cowboy_req:reply(400, Req)};
|
||||
{error, badrequest, path} ->
|
||||
{error, 400, Req};
|
||||
{halt, cowboy_req:reply(400, Req)};
|
||||
{error, notfound, path} ->
|
||||
{error, 404, Req}
|
||||
{halt, cowboy_req:reply(404, Req)}
|
||||
end.
|
||||
|
||||
%% Internal.
|
||||
|
|
|
@ -407,9 +407,7 @@ execute(Req, Env, [Middleware|Tail]) ->
|
|||
erlang:hibernate(?MODULE, resume,
|
||||
[Env, Tail, Module, Function, Args]);
|
||||
{halt, Req2} ->
|
||||
cowboy_req:ensure_response(Req2, 204);
|
||||
{error, Status, Req2} ->
|
||||
cowboy_req:reply(Status, Req2)
|
||||
cowboy_req:ensure_response(Req2, 204)
|
||||
end.
|
||||
|
||||
-spec resume(cowboy_middleware:env(), [module()],
|
||||
|
@ -422,9 +420,7 @@ resume(Env, Tail, Module, Function, Args) ->
|
|||
erlang:hibernate(?MODULE, resume,
|
||||
[Env, Tail, Module2, Function2, Args2]);
|
||||
{halt, Req2} ->
|
||||
cowboy_req:ensure_response(Req2, 204);
|
||||
{error, Status, Req2} ->
|
||||
cowboy_req:reply(Status, Req2)
|
||||
cowboy_req:ensure_response(Req2, 204)
|
||||
end.
|
||||
|
||||
%% Reply functions used by cowboy_req.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue