PageRenderTime 35ms CodeModel.GetById 15ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/deps/webmachine/src/webmachine_error_handler.erl

https://code.google.com/p/zotonic/
Erlang | 72 lines | 45 code | 10 blank | 17 comment | 1 complexity | 1dfc645ef61cc4dff4fb0136f51f0a8d MD5 | raw file
 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
28render_error(Code, ReqData, Reason) ->
29    case webmachine_request:has_response_body(ReqData) of
30        {true,_} -> {webmachine_request:response_body(ReqData), ReqData};
31        {false,_} -> render_error_body(Code, ReqData, Reason)
32    end.
33
34render_error_body(404, ReqData, _Reason) ->
35    ReqData1 = wrq:set_resp_header("Content-Type", "text/html", ReqData),
36    {<<"<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>">>, ReqData1};
37
38render_error_body(500, ReqData, Reason) ->
39    ReqData1 = wrq:set_resp_header("Content-Type", "text/html", ReqData),
40    Path = wrq:path(ReqData),
41    error_logger:error_msg("webmachine error: path=~p~n~p~n", [Path, Reason]),
42    STString = io_lib:format("~p", [Reason]),
43    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>",
44    ErrorEnd = "</pre><P><HR><ADDRESS>mochiweb+webmachine web server</ADDRESS></body></html>",
45    ErrorIOList = [ErrorStart,STString,ErrorEnd],
46    {erlang:iolist_to_binary(ErrorIOList), ReqData1};
47
48render_error_body(501, ReqData, _Reason) ->
49    ReqData1 = wrq:set_resp_header("Content-Type", "text/html", ReqData),
50    Method = wrq:method(ReqData),
51    error_logger:error_msg("Webmachine does not support method ~p~n", [Method]),
52    ErrorStr = io_lib:format("<html><head><title>501 Not Implemented</title>"
53                             "</head><body><h1>Internal Server Error</h1>"
54                             "The server does not support the ~p method.<br>"
55                             "<P><HR><ADDRESS>mochiweb+webmachine web server"
56                             "</ADDRESS></body></html>",
57                             [Method]),
58    {erlang:iolist_to_binary(ErrorStr), ReqData1};
59
60render_error_body(503, ReqData, _Reason) ->
61    ReqData1 = wrq:set_resp_header("Content-Type", "text/html", ReqData),
62    error_logger:error_msg("Webmachine cannot fulfill"
63                           " the request at this time"),
64    ErrorStr = "<html><head><title>503 Service Unavailable</title>"
65               "</head><body><h1>Service Unavailable</h1>"
66               "The server is currently unable to handle "
67               "the request due to a temporary overloading "
68               "or maintenance of the server.<br>"
69               "<P><HR><ADDRESS>mochiweb+webmachine web server"
70               "</ADDRESS></body></html>",
71    {list_to_binary(ErrorStr), ReqData1}.
72