error_hook_responder.erl 834 B

123456789101112131415161718192021
  1. %% Feel free to use, reuse and abuse the code in this file.
  2. -module(error_hook_responder).
  3. -export([respond/4]).
  4. respond(404, Headers, <<>>, Req) ->
  5. {Path, Req2} = cowboy_req:path(Req),
  6. Body = <<"404 Not Found: \"", Path/binary, "\" is not the path you are looking for.\n">>,
  7. Headers2 = lists:keyreplace(<<"content-length">>, 1, Headers,
  8. {<<"content-length">>, integer_to_list(byte_size(Body))}),
  9. {ok, Req3} = cowboy_req:reply(404, Headers2, Body, Req2),
  10. Req3;
  11. respond(Code, Headers, <<>>, Req) when is_integer(Code), Code >= 400 ->
  12. Body = ["HTTP Error ", integer_to_list(Code), $\n],
  13. Headers2 = lists:keyreplace(<<"content-length">>, 1, Headers,
  14. {<<"content-length">>, integer_to_list(iolist_size(Body))}),
  15. {ok, Req2} = cowboy_req:reply(Code, Headers2, Body, Req),
  16. Req2;
  17. respond(_Code, _Headers, _Body, Req) ->
  18. Req.