Skip to content
This repository has been archived by the owner on Jan 9, 2020. It is now read-only.

Commit

Permalink
Added kwargs to get_error_html in the RequestHandler so that downstre…
Browse files Browse the repository at this point in the history
…am actors can render or otherwise use the exception object thrown. This is a backwards-incompatible change for anyone who has overridden get_error_html().
  • Loading branch information
massless committed Dec 14, 2009
1 parent 4381df0 commit 22d840e
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions tornado/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ def finish(self, chunk=None):
self._log()
self._finished = True

def send_error(self, status_code=500):
def send_error(self, status_code=500, **kwargs):
"""Sends the given HTTP error code to the browser.
We also send the error HTML for the given error code as returned by
Expand All @@ -470,11 +470,15 @@ def send_error(self, status_code=500):
return
self.clear()
self.set_status(status_code)
message = self.get_error_html(status_code)
message = self.get_error_html(status_code, **kwargs)
self.finish(message)

def get_error_html(self, status_code):
"""Override to implement custom error pages."""
def get_error_html(self, status_code, **kwargs):
"""Override to implement custom error pages.
If this error was caused by an uncaught exception, the
exception object can be found in kwargs e.g. kwargs['exception']
"""
return "<html><title>%(code)d: %(message)s</title>" \
"<body>%(code)d: %(message)s</body></html>" % {
"code": status_code,
Expand All @@ -496,7 +500,7 @@ def locale(self):
self._locale = self.get_browser_locale()
assert self._locale
return self._locale

def get_user_locale(self):
"""Override to determine the locale from the authenticated user.
Expand Down Expand Up @@ -681,7 +685,7 @@ def _execute(self, transforms, *args, **kwargs):
self.application.settings.get("xsrf_cookies"):
self.check_xsrf_cookie()
self.prepare()
if not self._finished:
if not self._finished:
getattr(self, self.request.method.lower())(*args, **kwargs)
if self._auto_finish and not self._finished:
self.finish()
Expand Down Expand Up @@ -720,13 +724,13 @@ def _handle_request_exception(self, e):
logging.warning(format, *args)
if e.status_code not in httplib.responses:
logging.error("Bad HTTP status code: %d", e.status_code)
self.send_error(500)
self.send_error(500, exception=e)
else:
self.send_error(e.status_code)
self.send_error(e.status_code, exception=e)
else:
logging.error("Uncaught exception %s\n%r", self._request_summary(),
self.request, exc_info=e)
self.send_error(500)
self.send_error(500, exception=e)

def _ui_module(self, name, module):
def render(*args, **kwargs):
Expand Down Expand Up @@ -951,7 +955,7 @@ def __call__(self, request):
handler = None
args = []
handlers = self._get_host_handlers(request)
if not handlers:
if not handlers:
handler = RedirectHandler(
request, "http://" + self.default_host + "/")
else:
Expand Down Expand Up @@ -1021,7 +1025,7 @@ def __init__(self, application, request, url, permanent=True):
RequestHandler.__init__(self, application, request)
self._url = url
self._permanent = permanent

def get(self):
self.redirect(self._url, permanent=self._permanent)

Expand Down Expand Up @@ -1196,7 +1200,7 @@ def transform_first_chunk(self, headers, chunk, finishing):
headers["Transfer-Encoding"] = "chunked"
chunk = self.transform_chunk(chunk, finishing)
return headers, chunk

def transform_chunk(self, block, finishing):
if self._chunking:
# Don't write out empty chunks because that means END-OF-STREAM
Expand Down Expand Up @@ -1271,12 +1275,12 @@ def __init__(self, pattern, handler_class, kwargs={}, name=None):
Parameters:
pattern: Regular expression to be matched. Any groups in the regex
will be passed in to the handler's get/post/etc methods as
will be passed in to the handler's get/post/etc methods as
arguments.
handler_class: RequestHandler subclass to be invoked.
kwargs (optional): A dictionary of additional arguments to be passed
to the handler's constructor.
name (optional): A name for this handler. Used by
name (optional): A name for this handler. Used by
Application.reverse_url.
"""
if not pattern.endswith('$'):
Expand Down

0 comments on commit 22d840e

Please sign in to comment.