From 86c5a569d572e0f5afcf6cfbe8e1935cd69d21bd Mon Sep 17 00:00:00 2001 From: Raphael Deem Date: Tue, 7 Mar 2017 16:22:23 -0800 Subject: [PATCH] allow exceptions to be a list --- sanic/app.py | 6 +++++- tests/test_exceptions.py | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/sanic/app.py b/sanic/app.py index bb7b0efe..46166ab6 100644 --- a/sanic/app.py +++ b/sanic/app.py @@ -227,7 +227,11 @@ class Sanic: def response(handler): for exception in exceptions: - self.error_handler.add(exception, handler) + if isinstance(exception, (tuple, list)): + for e in exception: + self.error_handler.add(e, handler) + else: + self.error_handler.add(exception, handler) return handler return response diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py index 968369f8..c557b202 100644 --- a/tests/test_exceptions.py +++ b/tests/test_exceptions.py @@ -44,6 +44,21 @@ def exception_app(): return app +def test_catch_exception_list(): + app = Sanic('exception_list') + @app.exception([SanicExceptionTestException, NotFound]) + def exception_list(request, exception): + return text("ok") + + @app.route('/') + def exception(request): + raise SanicExceptionTestException("You won't see me") + + request, response = app.test_client.get('/random') + assert response.text == 'ok' + + request, response = app.test_client.get('/') + assert response.text == 'ok' def test_no_exception(exception_app): """Test that a route works without an exception"""