]> review.fuel-infra Code Review - packages/trusty/rabbitmq-server.git/blob
7f4eb5905e3ce6ac73c44bf1a3ab4886dd3b5b96
[packages/trusty/rabbitmq-server.git] /
1 %% @author Justin Sheehy <justin@basho.com>
2 %% @author Andy Gross <andy@basho.com>
3 %% @author Jeremy Latt <jeremy@basho.com>
4 %% @copyright 2007-2008 Basho Technologies
5 %%
6 %%    Licensed under the Apache License, Version 2.0 (the "License");
7 %%    you may not use this file except in compliance with the License.
8 %%    You may obtain a copy of the License at
9 %%
10 %%        http://www.apache.org/licenses/LICENSE-2.0
11 %%
12 %%    Unless required by applicable law or agreed to in writing, software
13 %%    distributed under the License is distributed on an "AS IS" BASIS,
14 %%    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 %%    See the License for the specific language governing permissions and
16 %%    limitations under the License.
17
18
19 %% @doc Some fairly minimal error message formatters.
20
21 -module(webmachine_error_handler).
22 -author('Justin Sheehy <justin@basho.com>').
23 -author('Andy Gross <andy@basho.com>').
24 -author('Jeremy Latt <jeremy@basho.com>').
25
26 -export([render_error/3]).
27
28 render_error(Code, Req, Reason) ->
29     case Req:has_response_body() of
30         {true,_} ->
31             maybe_log(Req, Reason),
32             Req:response_body();
33         {false,_} -> render_error_body(Code, Req:trim_state(), Reason)
34     end.
35
36 render_error_body(404, Req, _Reason) ->
37     {ok, ReqState} = Req:add_response_header("Content-Type", "text/html"),
38     {<<"<HTML><HEAD><TITLE>404 Not Found</TITLE></HEAD><BODY><H1>Not Found</H1>The requested document was not found on this server.<P><HR><ADDRESS>mochiweb+webmachine web server</ADDRESS></BODY></HTML>">>, ReqState};
39
40 render_error_body(500, Req, Reason) ->
41     {ok, ReqState} = Req:add_response_header("Content-Type", "text/html"),
42     maybe_log(Req, Reason),
43     STString = io_lib:format("~p", [Reason]),
44     ErrorStart = "<html><head><title>500 Internal Server Error</title></head><body><h1>Internal Server Error</h1>The server encountered an error while processing this request:<br><pre>",
45     ErrorEnd = "</pre><P><HR><ADDRESS>mochiweb+webmachine web server</ADDRESS></body></html>",
46     ErrorIOList = [ErrorStart,STString,ErrorEnd],
47     {erlang:iolist_to_binary(ErrorIOList), ReqState};
48
49 render_error_body(501, Req, _Reason) ->
50     {ok, ReqState} = Req:add_response_header("Content-Type", "text/html"),
51     {Method,_} = Req:method(),
52     error_logger:error_msg("Webmachine does not support method ~p~n",
53                            [Method]),
54     ErrorStr = io_lib:format("<html><head><title>501 Not Implemented</title>"
55                              "</head><body><h1>Not Implemented</h1>"
56                              "The server does not support the ~p method.<br>"
57                              "<P><HR><ADDRESS>mochiweb+webmachine web server"
58                              "</ADDRESS></body></html>",
59                              [Method]),
60     {erlang:iolist_to_binary(ErrorStr), ReqState};
61
62 render_error_body(503, Req, _Reason) ->
63     {ok, ReqState} = Req:add_response_header("Content-Type", "text/html"),
64     error_logger:error_msg("Webmachine cannot fulfill"
65                            " the request at this time"),
66     ErrorStr = "<html><head><title>503 Service Unavailable</title>"
67                "</head><body><h1>Service Unavailable</h1>"
68                "The server is currently unable to handle "
69                "the request due to a temporary overloading "
70                "or maintenance of the server.<br>"
71                "<P><HR><ADDRESS>mochiweb+webmachine web server"
72                "</ADDRESS></body></html>",
73     {list_to_binary(ErrorStr), ReqState};
74
75 render_error_body(Code, Req, Reason) ->
76     {ok, ReqState} = Req:add_response_header("Content-Type", "text/html"),
77     ReasonPhrase = httpd_util:reason_phrase(Code),
78     Body = ["<html><head><title>",
79             integer_to_list(Code),
80             " ",
81             ReasonPhrase,
82             "</title></head><body><h1>",
83             ReasonPhrase,
84             "</h1>",
85             Reason,
86             "<p><hr><address>mochiweb+webmachine web server</address></body></html>"],
87     {iolist_to_binary(Body), ReqState}.
88
89 maybe_log(_Req, {error, {exit, normal, _Stack}}) ->
90     %% webmachine_request did an exit(normal), so suppress this
91     %% message. This usually happens when a chunked upload is
92     %% interrupted by network failure.
93     ok;
94 maybe_log(Req, Reason) ->
95     {Path,_} = Req:path(),
96     error_logger:error_msg("webmachine error: path=~p~n~p~n", [Path, Reason]).