Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/elli_middleware.erl
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,18 @@
handle(CleanReq, Config) ->
Mods = mods(Config),
PreReq = preprocess(CleanReq, Mods),
Res = process(PreReq, Mods),
postprocess(PreReq, Res, lists:reverse(Mods)).
try process(PreReq, Mods) of
Res -> postprocess(PreReq, Res, lists:reverse(Mods))
catch
throw:{ResponseCode, Headers, Body} when is_integer(ResponseCode) ->
postprocess(PreReq, {ResponseCode, Headers, Body}, lists:reverse(Mods));
throw:Exc ->
postprocess(PreReq, {500, [], <<"Internal server error">>}, lists:reverse(Mods));
error:Error ->
postprocess(PreReq, {500, [], <<"Internal server error">>}, lists:reverse(Mods));
exit:Exit ->
postprocess(PreReq, {500, [], <<"Internal server error">>}, lists:reverse(Mods))
end.


handle_event(elli_startup, Args, Config) ->
Expand Down
29 changes: 29 additions & 0 deletions src/elli_middleware_error_responses.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
%% @doc: Add request reference to error response body as Elli middleware.
%% Postprocesses all requests and add request's unique reference to response
%% body.

-module(elli_middleware_error_responses).
-export([postprocess/3]).

%%
%% Postprocess handler
%%

postprocess(Req, {ResponseCode, Headers, Body} = Res, _Args)
when is_integer(ResponseCode) ->
case is_error(ResponseCode) of
false ->
Res;
true ->
Msg = io_lib:format("~nRequest: ~p", [elli_request:to_proplist(Req)]),
{ResponseCode, Headers, [Body, Msg]}
end;
postprocess(_, Res, _) ->
Res.

%%
%% INTERNALS
%%

is_error(ResponseCode) ->
ResponseCode >= 400 andalso ResponseCode < 600.
19 changes: 18 additions & 1 deletion test/elli_middleware_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ elli_test_() ->
[
?_test(hello_world()),
?_test(short_circuit()),
?_test(compress())
?_test(compress()),
?_test(error_responses())
]}.

%%
Expand Down Expand Up @@ -58,6 +59,21 @@ compress() ->
?assertEqual(lists:flatten(lists:duplicate(86, "Hello World!")), body(Response3)).


error_responses() ->
{ok, Response} = httpc:request("http://localhost:3002/foobarbaz"),
?assertEqual(404, status(Response)),
?assertMatch({match, _Captured}, re:run(body(Response), "Not Found")),
?assertMatch({match, _Captured}, re:run(body(Response), "Request: ")),

{ok, Response1} = httpc:request("http://localhost:3002/crash"),
?assertEqual(500, status(Response1)),
?assertMatch({match, _Captured}, re:run(body(Response1), "Internal server error")),
?assertMatch({match, _Captured}, re:run(body(Response1), "Request: ")),

{ok, Response2} = httpc:request("http://localhost:3002/403"),
?assertEqual(403, status(Response2)),
?assertMatch({match, _Captured}, re:run(body(Response2), "Forbidden")),
?assertMatch({match, _Captured}, re:run(body(Response2), "Request: ")).



Expand Down Expand Up @@ -88,6 +104,7 @@ setup() ->
{port, 514}]},
{elli_example_middleware, []},
{elli_middleware_compress, []},
{elli_middleware_error_responses, []},
{elli_example_callback, []}
]}
],
Expand Down