Skip to content

Instantly share code, notes, and snippets.

@jehiah
Created May 12, 2010 05:38
Show Gist options
  • Save jehiah/398252 to your computer and use it in GitHub Desktop.
Save jehiah/398252 to your computer and use it in GitHub Desktop.
import os
import httplib
import tornado.web
class ErrorHandler(tornado.web.RequestHandler):
"""Generates an error response with status_code for all requests."""
def __init__(self, application, request, status_code):
tornado.web.RequestHandler.__init__(self, application, request)
self.set_status(status_code)
def get_error_html(self, status_code, **kwargs):
self.require_setting("static_path")
if status_code in [404, 500, 503, 403]:
filename = os.path.join(self.settings['static_path'], '%d.html' % status_code)
if os.path.exists(filename):
f = open(filename, 'r')
data = f.read()
f.close()
return data
return "<html><title>%(code)d: %(message)s</title>" \
"<body class='bodyErrorPage'>%(code)d: %(message)s</body></html>" % {
"code": status_code,
"message": httplib.responses[status_code],
}
def prepare(self):
raise tornado.web.HTTPError(self._status_code)
## override the tornado.web.ErrorHandler with our default ErrorHandler
tornado.web.ErrorHandler = ErrorHandler
@rebelnz
Copy link

rebelnz commented Sep 23, 2012

very helpful - thanks

@shiny
Copy link

shiny commented Apr 27, 2013

Beautiful code!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment