diff options
| author | Jason R. Coombs <jaraco@jaraco.com> | 2019-10-02 19:40:28 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-10-02 19:40:28 -0400 |
| commit | f185ecd005d7fdbafb0ed83b0e49f05ac76e43fd (patch) | |
| tree | b73d0d9cac0b9e266fbef3bb2a091bd55cfe611c | |
| parent | fd26b216fbbb8381132455627148c2d4ad1240d8 (diff) | |
| parent | f4d261f471f094809b0d093e12770fecef953213 (diff) | |
| download | cherrypy-git-18.3.0.tar.gz | |
Merge pull request #1806 from cherrypy/feature/1770-log-all-errors-in-hooksv18.3.0
Emit all errors in hooks
| -rw-r--r-- | CHANGES.rst | 6 | ||||
| -rw-r--r-- | cherrypy/_cprequest.py | 52 |
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__() |
