diff --git a/sanic/exceptions.py b/sanic/exceptions.py index 1e8ec639..2596a97a 100644 --- a/sanic/exceptions.py +++ b/sanic/exceptions.py @@ -140,9 +140,9 @@ class PayloadTooLarge(SanicException): class Handler: handlers = None - def __init__(self, sanic): + def __init__(self): self.handlers = {} - self.sanic = sanic + self.debug = False def _render_traceback_html(self, exception, request): exc_type, exc_value, tb = sys.exc_info() @@ -175,7 +175,7 @@ class Handler: response = handler(request=request, exception=exception) except: log.error(format_exc()) - if self.sanic.debug: + if self.debug: response_message = ( 'Exception raised in exception handler "{}" ' 'for uri: "{}"\n{}').format( @@ -192,7 +192,7 @@ class Handler: return text( 'Error: {}'.format(exception), status=getattr(exception, 'status_code', 500)) - elif self.sanic.debug: + elif self.debug: html_output = self._render_traceback_html(exception, request) response_message = ( diff --git a/sanic/sanic.py b/sanic/sanic.py index e21fc556..cea09470 100644 --- a/sanic/sanic.py +++ b/sanic/sanic.py @@ -36,7 +36,7 @@ class Sanic: name = getmodulename(frame_records[1]) self.name = name self.router = router or Router() - self.error_handler = error_handler or Handler(self) + self.error_handler = error_handler or Handler() self.config = Config() self.request_middleware = deque() self.response_middleware = deque() @@ -300,7 +300,7 @@ class Sanic: :param protocol: Subclass of asyncio protocol class :return: Nothing """ - self.error_handler.debug = True + self.error_handler.debug = debug self.debug = debug self.loop = loop diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py index a81e0d09..819d39f2 100644 --- a/tests/test_exceptions.py +++ b/tests/test_exceptions.py @@ -84,10 +84,29 @@ def test_handled_unhandled_exception(exception_app): "The server encountered an internal error and " "cannot complete your request.") - def test_exception_in_exception_handler(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') 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=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 ')