Skip to content

Commit

Permalink
Remove the error tuple return value for middlewares
Browse files Browse the repository at this point in the history
It wasn't interesting compared to simply returning a halt tuple
with an explicit reply.
  • Loading branch information
essen committed Sep 24, 2014
1 parent aa4d86b commit c56bada
Show file tree
Hide file tree
Showing 8 changed files with 8 additions and 31 deletions.
5 changes: 0 additions & 5 deletions ROADMAP.md
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion doc/src/guide/middlewares.ezdoc
Expand Up @@ -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`
Expand Down
7 changes: 0 additions & 7 deletions doc/src/manual/cowboy_middleware.ezdoc
Expand Up @@ -22,7 +22,6 @@ optionally with its contents modified.
-> {ok, Req, Env}
| {suspend, Module, Function, Args}
| {halt, Req}
| {error, StatusCode, Req}

Types:

Expand All @@ -31,7 +30,6 @@ Types:
* Module = module()
* Function = atom()
* Args = [any()]
* StatusCode = cowboy:http_status()

Execute the middleware.

Expand All @@ -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.
1 change: 0 additions & 1 deletion src/cowboy_handler.erl
Expand Up @@ -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) ->
Expand Down
1 change: 0 additions & 1 deletion src/cowboy_middleware.erl
Expand Up @@ -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().
8 changes: 2 additions & 6 deletions src/cowboy_protocol.erl
Expand Up @@ -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()],
Expand All @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions src/cowboy_router.erl
Expand Up @@ -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),
Expand All @@ -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.
Expand Down
8 changes: 2 additions & 6 deletions src/cowboy_spdy.erl
Expand Up @@ -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()],
Expand All @@ -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.
Expand Down

0 comments on commit c56bada

Please sign in to comment.