Skip to content

Commit

Permalink
Fix: handle errors on event handlers (refs: #7629)
Browse files Browse the repository at this point in the history
  • Loading branch information
tk0miya committed May 16, 2020
1 parent e95567c commit b9793e5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ Bugs fixed
* #7628: imgconverter: runs imagemagick once unnecessary for builders not
supporting images
* #7610: incorrectly renders consecutive backslashes for docutils-0.16
* #7646: handle errors on event handlers

Testing
--------
Expand Down
18 changes: 12 additions & 6 deletions sphinx/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from typing import Any, Callable, Dict, List, NamedTuple

from sphinx.deprecation import RemovedInSphinx40Warning
from sphinx.errors import ExtensionError
from sphinx.errors import ExtensionError, SphinxError
from sphinx.locale import __
from sphinx.util import logging

Expand Down Expand Up @@ -100,11 +100,17 @@ def emit(self, name: str, *args: Any) -> List:
results = []
listeners = sorted(self.listeners[name], key=attrgetter("priority"))
for listener in listeners:
if self.app is None:
# for compatibility; RemovedInSphinx40Warning
results.append(listener.handler(*args))
else:
results.append(listener.handler(self.app, *args))
try:
if self.app is None:
# for compatibility; RemovedInSphinx40Warning
results.append(listener.handler(*args))
else:
results.append(listener.handler(self.app, *args))
except SphinxError:
raise
except Exception as exc:
raise ExtensionError(__("Handler %r for event %r threw an exception") %
(listener.handler, name)) from exc
return results

def emit_firstresult(self, name: str, *args: Any) -> Any:
Expand Down

0 comments on commit b9793e5

Please sign in to comment.