I can't recall which error it was (probably 404), but it wasn't being
handled by that method. So i had to load my own subclasses of a
couples of classes in. I've included them below:
The important bits to know are that we subclassed built in tornado
subclasses, and overrode methods like get_error_html on the
ErrorHandler, then monkey patched that into the application.
from tornado.web import Application as _Application, RedirectHandler,\
ErrorHandler as _ErrorHandler, RequestHandler, HTTPError
class ErrorHandler(_ErrorHandler):
"""Generates an error response with status_code for all requests."""
def __init__(self, application, request, status_code):
RequestHandler.__init__(self, application, request)
self.set_status(status_code)
def prepare(self):
raise HTTPError(self._status_code)
def get_error_html(self, status_code, **kwargs):
return open(os.path.join(settings.static_path,'error','404.html')).read()
class Application(_Application):
def __call__(self, request):
"""Called by HTTPServer to execute the request."""
transforms = [t(request) for t in self.transforms]
handler = None
args = []
kwargs = {}
handlers = self._get_host_handlers(request)
if not handlers:
handler = RedirectHandler(
request, "http://" + self.default_host + "/")
else:
for spec in handlers:
match = spec.regex.match(request.path)
if match:
handler = spec.handler_class(self, request, **spec.kwargs)
# Pass matched groups to the handler. Since
# match.groups() includes both named and unnamed groups,
# we want to use either groups or groupdict but not both.
kwargs = match.groupdict()
if kwargs:
args = []
else:
args = match.groups()
break
if not handler:
handler = ErrorHandler(self, request, 404)
# In debug mode, re-compile templates and reload static files on every
# request so you don't need to restart to see changes
if self.settings.get("debug"):
if getattr(RequestHandler, "_templates", None):
map(lambda loader: loader.reset(),
RequestHandler._templates.values())
RequestHandler._static_hashes = {}
handler._execute(transforms, *args, **kwargs)
return handler
application = Application(urlpatterns, **app_conf)