Created
May 12, 2010 05:38
-
-
Save jehiah/398252 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
very helpful - thanks
Beautiful code!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Worked perfectly, thanks for this :)