summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGES.rst6
-rw-r--r--cherrypy/_cprequest.py52
2 files changed, 37 insertions, 21 deletions
diff --git a/CHANGES.rst b/CHANGES.rst
index b205a85d..b3090082 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -1,3 +1,9 @@
+v18.3.0
+-------
+
+* :pr:`1806`: Support handling multiple exceptions when processing hooks as
+ reported in :issue:`1770`.
+
v18.2.0
-------
diff --git a/cherrypy/_cprequest.py b/cherrypy/_cprequest.py
index aa42f428..9b86bd67 100644
--- a/cherrypy/_cprequest.py
+++ b/cherrypy/_cprequest.py
@@ -1,5 +1,7 @@
import sys
import time
+import collections
+import operator
from http.cookies import SimpleCookie, CookieError
import uuid
@@ -90,28 +92,36 @@ class HookMap(dict):
def run(self, point):
"""Execute all registered Hooks (callbacks) for the given point."""
- exc = None
- hooks = self[point]
- hooks.sort()
+ self.run_hooks(iter(sorted(self[point])))
+
+ @classmethod
+ def run_hooks(cls, hooks):
+ """Execute the indicated hooks, trapping errors.
+
+ Hooks with ``.failsafe == True`` are guaranteed to run
+ even if others at the same hookpoint fail. In this case,
+ log the failure and proceed on to the next hook. The only
+ way to stop all processing from one of these hooks is
+ to raise a BaseException like SystemExit or
+ KeyboardInterrupt and stop the whole server.
+ """
+ assert isinstance(hooks, collections.abc.Iterator)
+ quiet_errors = (
+ cherrypy.HTTPError,
+ cherrypy.HTTPRedirect,
+ cherrypy.InternalRedirect,
+ )
+ safe = filter(operator.attrgetter('failsafe'), hooks)
for hook in hooks:
- # Some hooks are guaranteed to run even if others at
- # the same hookpoint fail. We will still log the failure,
- # but proceed on to the next hook. The only way
- # to stop all processing from one of these hooks is
- # to raise SystemExit and stop the whole server.
- if exc is None or hook.failsafe:
- try:
- hook()
- except (KeyboardInterrupt, SystemExit):
- raise
- except (cherrypy.HTTPError, cherrypy.HTTPRedirect,
- cherrypy.InternalRedirect):
- exc = sys.exc_info()[1]
- except Exception:
- exc = sys.exc_info()[1]
- cherrypy.log(traceback=True, severity=40)
- if exc:
- raise exc
+ try:
+ hook()
+ except quiet_errors:
+ cls.run_hooks(safe)
+ raise
+ except Exception:
+ cherrypy.log(traceback=True, severity=40)
+ cls.run_hooks(safe)
+ raise
def __copy__(self):
newmap = self.__class__()