/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. %% @doc Some fairly minimal error message formatters.
  18. -module(webmachine_error_handler).
  19. -author('Justin Sheehy <justin@basho.com>').
  20. -author('Andy Gross <andy@basho.com>').
  21. -author('Jeremy Latt <jeremy@basho.com>').
  22. -export([render_error/3]).
  23. render_error(Code, ReqData, Reason) ->
  24. case webmachine_request:has_response_body(ReqData) of
  25. {true,_} -> {webmachine_request:response_body(ReqData), ReqData};
  26. {false,_} -> render_error_body(Code, ReqData, Reason)
  27. end.
  28. render_error_body(404, ReqData, _Reason) ->
  29. ReqData1 = wrq:set_resp_header("Content-Type", "text/html", ReqData),
  30. {<<"<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};
  31. render_error_body(500, ReqData, Reason) ->
  32. ReqData1 = wrq:set_resp_header("Content-Type", "text/html", ReqData),
  33. Path = wrq:path(ReqData),
  34. error_logger:error_msg("webmachine error: path=~p~n~p~n", [Path, Reason]),
  35. STString = io_lib:format("~p", [Reason]),
  36. 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>",
  37. ErrorEnd = "</pre><P><HR><ADDRESS>mochiweb+webmachine web server</ADDRESS></body></html>",
  38. ErrorIOList = [ErrorStart,STString,ErrorEnd],
  39. {erlang:iolist_to_binary(ErrorIOList), ReqData1};
  40. render_error_body(501, ReqData, _Reason) ->
  41. ReqData1 = wrq:set_resp_header("Content-Type", "text/html", ReqData),
  42. Method = wrq:method(ReqData),
  43. error_logger:error_msg("Webmachine does not support method ~p~n", [Method]),
  44. ErrorStr = io_lib:format("<html><head><title>501 Not Implemented</title>"
  45. "</head><body><h1>Internal Server Error</h1>"
  46. "The server does not support the ~p method.<br>"
  47. "<P><HR><ADDRESS>mochiweb+webmachine web server"
  48. "</ADDRESS></body></html>",
  49. [Method]),
  50. {erlang:iolist_to_binary(ErrorStr), ReqData1};
  51. render_error_body(503, ReqData, _Reason) ->
  52. ReqData1 = wrq:set_resp_header("Content-Type", "text/html", ReqData),
  53. error_logger:error_msg("Webmachine cannot fulfill"
  54. " the request at this time"),
  55. ErrorStr = "<html><head><title>503 Service Unavailable</title>"
  56. "</head><body><h1>Service Unavailable</h1>"
  57. "The server is currently unable to handle "
  58. "the request due to a temporary overloading "
  59. "or maintenance of the server.<br>"
  60. "<P><HR><ADDRESS>mochiweb+webmachine web server"
  61. "</ADDRESS></body></html>",
  62. {list_to_binary(ErrorStr), ReqData1}.