Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always print tracebacks in logs when Sphinx encounters an internal error #13163

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ Features added
Patch by Chris Barrick.
* #13227: Implement the :rst:role:`kbd` role as a ``SphinxRole``.
Patch by Adam Turner.
* #13163: Always print tracebacks in full when Sphinx encounters an internal
error.
Patch by Kevin Deldycke.

Bugs fixed
----------
Expand Down
73 changes: 13 additions & 60 deletions sphinx/_cli/util/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,70 +176,23 @@ def print_red(*values: str) -> None:

print_err()
if print_traceback or use_pdb:
print_err(format_traceback(exception))
# format an exception with traceback, but only the last frame.
te = TracebackException.from_exception(exception, limit=-1)
formatted_tb = te.stack.format()[-1] + ''.join(te.format_exception_only()).rstrip()

print_red(__('Exception occurred:'))
print_err(formatted_tb)
traceback_output = full_exception_context(
exception, message_log=message_log, extensions=extensions
)
print_err(traceback_output)
traceback_info_path = write_temporary_file(traceback_output)
print_err(__('The full traceback has been saved in:'))
print_err(traceback_info_path)
print_err()

if use_pdb:
from pdb import post_mortem

print_red(__('Exception occurred, starting debugger:'))
post_mortem(exception.__traceback__)
return

if isinstance(exception, KeyboardInterrupt):
print_err(__('Interrupted!'))
return

if isinstance(exception, SystemMessage):
print_red(__('reStructuredText markup error:'))
print_err(str(exception))
return

if isinstance(exception, SphinxError):
print_red(f'{exception.category}:')
print_err(str(exception))
return

if isinstance(exception, UnicodeError):
print_red(__('Encoding error:'))
print_err(str(exception))
return

if isinstance(exception, RecursionError):
print_red(__('Recursion error:'))
print_err(str(exception))
print_err()
print_err(
__(
'This can happen with very large or deeply nested source '
'files. You can carefully increase the default Python '
'recursion limit of 1,000 in conf.py with e.g.:'
)
)
print_err('\n import sys\n sys.setrecursionlimit(1_500)\n')
return

# format an exception with traceback, but only the last frame.
te = TracebackException.from_exception(exception, limit=-1)
formatted_tb = te.stack.format()[-1] + ''.join(te.format_exception_only()).rstrip()

print_red(__('Exception occurred:'))
print_err(formatted_tb)
traceback_info_path = save_traceback(
exception, message_log=message_log, extensions=extensions
)
print_err(__('The full traceback has been saved in:'))
print_err(traceback_info_path)
print_err()
print_err(
__(
'To report this error to the developers, please open an issue '
'at <https://github.com/sphinx-doc/sphinx/issues/>. Thanks!'
)
)
print_err(
__(
'Please also report this if it was a user error, so '
'that a better error message can be provided next time.'
)
)