test case added, indent fixed

This commit is contained in:
Benny 2017-05-08 23:53:41 +08:00
parent 3f0e662358
commit 1a2a0931ed
2 changed files with 22 additions and 4 deletions

View File

@ -57,7 +57,7 @@ class ErrorHandler:
def add(self, exception, handler): def add(self, exception, handler):
self.handlers.append((exception, handler)) self.handlers.append((exception, handler))
def add_base(self, exception, handler): def add_base(self, exception, handler):
self.handlers_base.append((exception, handler)) self.handlers_base.append((exception, handler))
@ -71,12 +71,12 @@ class ErrorHandler:
for exception_class, handler in self.handlers_base: for exception_class, handler in self.handlers_base:
try: try:
raise raise
except exception_class: except exception_class:
return handler return handler
except Exception: except Exception:
pass pass
self.cached_handlers[type(exception)] = None self.cached_handlers[type(exception)] = None
handler = None handler = None
return handler return handler

View File

@ -43,12 +43,25 @@ def handler_6(request, arg):
raise e from ValueError("{}".format(arg)) raise e from ValueError("{}".format(arg))
return text(foo) return text(foo)
@exception_handler_app.route('/7')
def handler_7(request):
# raise an IndexError
a = [1,2,3]
return text( a[3] )
@exception_handler_app.exception(NotFound, ServerError)
def handler_exception(request, exception):
return text("OK")
@exception_handler_app.exception(NotFound, ServerError) @exception_handler_app.exception(NotFound, ServerError)
def handler_exception(request, exception): def handler_exception(request, exception):
return text("OK") return text("OK")
@exception_handler_app.exception_base( LookupError )
def handler_exception(request, exception):
# LookupError can catch IndexError
return text("Caught by LookupError")
def test_invalid_usage_exception_handler(): def test_invalid_usage_exception_handler():
request, response = exception_handler_app.test_client.get('/1') request, response = exception_handler_app.test_client.get('/1')
assert response.status == 400 assert response.status == 400
@ -112,6 +125,11 @@ def test_chained_exception_handler():
"ZeroDivisionError: division by zero " "ZeroDivisionError: division by zero "
"while handling path /6/0") == summary_text "while handling path /6/0") == summary_text
def test_chained_exception_base_handler():
request, response = exception_handler_app.test_client.get(
'/7', debug=True)
assert response.status == 200
assert response.body == b'Caught by LookupError'
def test_exception_handler_lookup(): def test_exception_handler_lookup():
class CustomError(Exception): class CustomError(Exception):