Skip to content

Commit 026a500

Browse files
committed
Improve errors with current document information (#71)
1 parent 2325788 commit 026a500

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed

docs/src/release_notes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Unreleased
1313
- Declare CSS class as public API (:issue:`3`)
1414
- Add ability to link to subclass documentation (:issue:`68`)
1515
- Append a newline to error messages with source code (:issue:`70`)
16+
- Improve errors with information about the current document (:issue:`71`)
1617

1718
0.6.0 (2021-11-21)
1819
------------------

src/sphinx_codeautolink/extension/__init__.py

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,35 @@ class DocumentedObject:
2525
return_type: str = None
2626

2727

28-
def print_exceptions(func):
28+
def print_exceptions(append_source: bool = False):
2929
"""
3030
Print the traceback of uncaught and unexpected exceptions.
3131
3232
This is done because the Sphinx process masks the traceback
3333
and only displays the main error message making debugging difficult.
34+
If append_source is set, information about the currently processed document
35+
is pulled from the second argument named "doctree" and added to the message.
3436
"""
35-
@wraps(func)
36-
def wrapper(*args, **kwargs):
37-
try:
38-
return func(*args, **kwargs)
39-
except (UserError, ParsingError):
40-
raise
41-
except Exception:
42-
print_exc()
43-
raise
44-
return wrapper
37+
def decorator(func):
38+
@wraps(func)
39+
def wrapper(*args, **kwargs):
40+
try:
41+
return func(*args, **kwargs)
42+
except (UserError, ParsingError):
43+
raise
44+
except Exception as e:
45+
print_exc()
46+
if append_source:
47+
doctree = args[2] if len(args) > 1 else kwargs['doctree']
48+
source = doctree['source']
49+
msg = f'in document `{source}`'
50+
if e.args:
51+
e.args = (e.args[0] + f' ({msg})',) + e.args[1:]
52+
else:
53+
e.args = (f'Unexpected error {msg}',)
54+
raise
55+
return wrapper
56+
return decorator
4557

4658

4759
class SphinxCodeAutoLink:
@@ -58,7 +70,7 @@ def __init__(self):
5870
self.concat_default = None
5971
self.search_css_classes = None
6072

61-
@print_exceptions
73+
@print_exceptions()
6274
def build_inited(self, app):
6375
"""Handle initial setup."""
6476
if app.builder.name != 'html':
@@ -81,7 +93,7 @@ def build_inited(self, app):
8193
if preface:
8294
self.global_preface = preface.split('\n')
8395

84-
@print_exceptions
96+
@print_exceptions()
8597
def autodoc_process_docstring(self, app, what, name, obj, options, lines):
8698
"""Handle autodoc-process-docstring event."""
8799
if self.do_nothing:
@@ -92,7 +104,7 @@ def autodoc_process_docstring(self, app, what, name, obj, options, lines):
92104
lines.append('.. autolink-examples:: ' + name)
93105
lines.append(' :collapse:')
94106

95-
@print_exceptions
107+
@print_exceptions(append_source=True)
96108
def parse_blocks(self, app, doctree):
97109
"""Parse code blocks for later link substitution."""
98110
if self.do_nothing:
@@ -108,7 +120,7 @@ def parse_blocks(self, app, doctree):
108120
doctree.walkabout(visitor)
109121
self.cache.transforms[visitor.current_document] = visitor.source_transforms
110122

111-
@print_exceptions
123+
@print_exceptions(append_source=True)
112124
def generate_backref_tables(self, app, doctree, docname):
113125
"""Generate backreference tables."""
114126
self.once_on_doctree_resolved(app)
@@ -166,7 +178,7 @@ def make_inventory(self, app):
166178
self._inventory = transposed
167179
return self._inventory
168180

169-
@print_exceptions
181+
@print_exceptions()
170182
def apply_links(self, app, exception):
171183
"""Apply links to HTML output and write refs file."""
172184
if self.do_nothing or exception is not None:

0 commit comments

Comments
 (0)