def get(self):
self.set_status(401)
self.set_header('WWW-Authenticate', 'Basic realm="something"')
- self.write('authentication required')
- raise Finish()
+ if self.get_argument('finish_value', ''):
+ raise Finish('authentication required')
+ else:
+ self.write('authentication required')
+ raise Finish()
def test_finish_exception(self):
- response = self.fetch('/')
- self.assertEqual(response.code, 401)
- self.assertEqual('Basic realm="something"',
- response.headers.get('WWW-Authenticate'))
- self.assertEqual(b'authentication required', response.body)
+ for url in ['/', '/?finish_value=1']:
+ response = self.fetch(url)
+ self.assertEqual(response.code, 401)
+ self.assertEqual('Basic realm="something"',
+ response.headers.get('WWW-Authenticate'))
+ self.assertEqual(b'authentication required', response.body)
@wsgi_safe
if isinstance(e, Finish):
# Not an error; just finish the request without logging.
if not self._finished:
- self.finish()
+ self.finish(*e.args)
return
try:
self.log_exception(*sys.exc_info())
class Finish(Exception):
"""An exception that ends the request without producing an error response.
- When `Finish` is raised in a `RequestHandler`, the request will end
- (calling `RequestHandler.finish` if it hasn't already been called),
- but the outgoing response will not be modified and the error-handling
- methods (including `RequestHandler.write_error`) will not be called.
+ When `Finish` is raised in a `RequestHandler`, the request will
+ end (calling `RequestHandler.finish` if it hasn't already been
+ called), but the error-handling methods (including
+ `RequestHandler.write_error`) will not be called.
+
+ If `Finish()` was created with no arguments, the pending response
+ will be sent as-is. If `Finish()` was given an argument, that
+ argument will be passed to `RequestHandler.finish()`.
This can be a more convenient way to implement custom error pages
than overriding ``write_error`` (especially in library code)::
self.set_status(401)
self.set_header('WWW-Authenticate', 'Basic realm="something"')
raise Finish()
+
+ .. versionchanged:: 4.3
+ Arguments passed to ``Finish()`` will be passed on to
+ `RequestHandler.finish`.
"""
pass