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
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
10 %% http://www.apache.org/licenses/LICENSE-2.0
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.
19 %% @doc Some fairly minimal error message formatters.
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>').
26 -export([render_error/3]).
28 render_error(Code, Req, Reason) ->
29 case Req:has_response_body() of
31 maybe_log(Req, Reason),
33 {false,_} -> render_error_body(Code, Req:trim_state(), Reason)
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};
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};
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",
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>",
60 {erlang:iolist_to_binary(ErrorStr), ReqState};
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};
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),
82 "</title></head><body><h1>",
86 "<p><hr><address>mochiweb+webmachine web server</address></body></html>"],
87 {iolist_to_binary(Body), ReqState}.
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.
94 maybe_log(Req, Reason) ->
95 {Path,_} = Req:path(),
96 error_logger:error_msg("webmachine error: path=~p~n~p~n", [Path, Reason]).