Better exception handling (#2128)
* WIP for better exception handling * Note about removal * resolve conditional to reduce lookups * Cleanup logic
This commit is contained in:
parent
5bb9aa0c2c
commit
80fca9aef7
|
@ -25,7 +25,6 @@ class ErrorHandler:
|
||||||
|
|
||||||
handlers = None
|
handlers = None
|
||||||
cached_handlers = None
|
cached_handlers = None
|
||||||
_missing = object()
|
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.handlers = []
|
self.handlers = []
|
||||||
|
@ -45,7 +44,9 @@ class ErrorHandler:
|
||||||
|
|
||||||
:return: None
|
:return: None
|
||||||
"""
|
"""
|
||||||
|
# self.handlers to be deprecated and removed in version 21.12
|
||||||
self.handlers.append((exception, handler))
|
self.handlers.append((exception, handler))
|
||||||
|
self.cached_handlers[exception] = handler
|
||||||
|
|
||||||
def lookup(self, exception):
|
def lookup(self, exception):
|
||||||
"""
|
"""
|
||||||
|
@ -61,14 +62,19 @@ class ErrorHandler:
|
||||||
|
|
||||||
:return: Registered function if found ``None`` otherwise
|
:return: Registered function if found ``None`` otherwise
|
||||||
"""
|
"""
|
||||||
handler = self.cached_handlers.get(type(exception), self._missing)
|
exception_class = type(exception)
|
||||||
if handler is self._missing:
|
if exception_class in self.cached_handlers:
|
||||||
for exception_class, handler in self.handlers:
|
return self.cached_handlers[exception_class]
|
||||||
if isinstance(exception, exception_class):
|
|
||||||
self.cached_handlers[type(exception)] = handler
|
for ancestor in type.mro(exception_class):
|
||||||
return handler
|
if ancestor in self.cached_handlers:
|
||||||
self.cached_handlers[type(exception)] = None
|
handler = self.cached_handlers[ancestor]
|
||||||
handler = None
|
self.cached_handlers[exception_class] = handler
|
||||||
|
return handler
|
||||||
|
if ancestor is BaseException:
|
||||||
|
break
|
||||||
|
self.cached_handlers[exception_class] = None
|
||||||
|
handler = None
|
||||||
return handler
|
return handler
|
||||||
|
|
||||||
def response(self, request, exception):
|
def response(self, request, exception):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user