Merge pull request #524 from r0fls/exception-list

allow exceptions to be a list
This commit is contained in:
Raphael Deem 2017-03-10 00:35:38 -08:00 committed by GitHub
commit c9ce33dfe6
2 changed files with 20 additions and 1 deletions

View File

@ -227,7 +227,11 @@ class Sanic:
def response(handler): def response(handler):
for exception in exceptions: 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 handler
return response return response

View File

@ -44,6 +44,21 @@ def exception_app():
return 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): def test_no_exception(exception_app):
"""Test that a route works without an exception""" """Test that a route works without an exception"""