set error handler debug from run debug arg

This commit is contained in:
Raphael Deem 2017-01-24 17:18:58 -08:00
parent 049a5a7493
commit 28f7abd1f8
3 changed files with 26 additions and 7 deletions

View File

@ -140,9 +140,9 @@ class PayloadTooLarge(SanicException):
class Handler: class Handler:
handlers = None handlers = None
def __init__(self, sanic): def __init__(self):
self.handlers = {} self.handlers = {}
self.sanic = sanic self.debug = False
def _render_traceback_html(self, exception, request): def _render_traceback_html(self, exception, request):
exc_type, exc_value, tb = sys.exc_info() exc_type, exc_value, tb = sys.exc_info()
@ -175,7 +175,7 @@ class Handler:
response = handler(request=request, exception=exception) response = handler(request=request, exception=exception)
except: except:
log.error(format_exc()) log.error(format_exc())
if self.sanic.debug: if self.debug:
response_message = ( response_message = (
'Exception raised in exception handler "{}" ' 'Exception raised in exception handler "{}" '
'for uri: "{}"\n{}').format( 'for uri: "{}"\n{}').format(
@ -192,7 +192,7 @@ class Handler:
return text( return text(
'Error: {}'.format(exception), 'Error: {}'.format(exception),
status=getattr(exception, 'status_code', 500)) status=getattr(exception, 'status_code', 500))
elif self.sanic.debug: elif self.debug:
html_output = self._render_traceback_html(exception, request) html_output = self._render_traceback_html(exception, request)
response_message = ( response_message = (

View File

@ -36,7 +36,7 @@ class Sanic:
name = getmodulename(frame_records[1]) name = getmodulename(frame_records[1])
self.name = name self.name = name
self.router = router or Router() self.router = router or Router()
self.error_handler = error_handler or Handler(self) self.error_handler = error_handler or Handler()
self.config = Config() self.config = Config()
self.request_middleware = deque() self.request_middleware = deque()
self.response_middleware = deque() self.response_middleware = deque()
@ -300,7 +300,7 @@ class Sanic:
:param protocol: Subclass of asyncio protocol class :param protocol: Subclass of asyncio protocol class
:return: Nothing :return: Nothing
""" """
self.error_handler.debug = True self.error_handler.debug = debug
self.debug = debug self.debug = debug
self.loop = loop self.loop = loop

View File

@ -84,10 +84,29 @@ def test_handled_unhandled_exception(exception_app):
"The server encountered an internal error and " "The server encountered an internal error and "
"cannot complete your request.") "cannot complete your request.")
def test_exception_in_exception_handler(exception_app): def test_exception_in_exception_handler(exception_app):
"""Test that an exception thrown in an error handler is handled""" """Test that an exception thrown in an error handler is handled"""
request, response = sanic_endpoint_test( request, response = sanic_endpoint_test(
exception_app, uri='/error_in_error_handler_handler') exception_app, uri='/error_in_error_handler_handler')
assert response.status == 500 assert response.status == 500
assert response.body == b'An error occurred while handling an error' assert response.body == b'An error occurred while handling an error'
def test_exception_in_exception_handler_debug_off(exception_app):
"""Test that an exception thrown in an error handler is handled"""
request, response = sanic_endpoint_test(
exception_app,
uri='/error_in_error_handler_handler',
debug=False)
assert response.status == 500
assert response.body == b'An error occurred while handling an error'
def test_exception_in_exception_handler_debug_off(exception_app):
"""Test that an exception thrown in an error handler is handled"""
request, response = sanic_endpoint_test(
exception_app,
uri='/error_in_error_handler_handler',
debug=True)
assert response.status == 500
assert response.body.startswith(b'Exception raised in exception ')