Skip to content

Commit

Permalink
Fix formatting of notes possible added to an Exception
Browse files Browse the repository at this point in the history
  • Loading branch information
Delgan committed Sep 9, 2023
1 parent 9da27db commit d70e7af
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
=============

- Add support for formatting of ``ExceptionGroup`` errors (`#805 <https://github.com/Delgan/loguru/issues/805>`_).
- Fix formatting of possible ``__notes__`` attached to an ``Exception`` (`#980 <https://github.com/Delgan/loguru/issues/980>`_).


`0.7.1`_ (2023-09-04)
Expand Down
14 changes: 12 additions & 2 deletions loguru/_better_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,17 @@ def _format_exception(
)
exception_only = traceback.format_exception_only(exc_type, exc_value)

error_message = exception_only[-1][:-1] # Remove last new line temporarily
# Determining the correct index for the "Exception: message" part in the formatted exception
# is challenging. This is because it might be preceded by multiple lines specific to
# "SyntaxError" or followed by various notes. However, we can make an educated guess based
# on the indentation; the preliminary context for "SyntaxError" is always indented, while
# the Exception itself is not. This allows us to identify the correct index for the
# exception message.
for error_message_index, part in enumerate(exception_only): # noqa: B007
if not part.startswith(" "):
break

error_message = exception_only[error_message_index][:-1] # Remove last new line temporarily

if self._colorize:
if ":" in error_message:
Expand All @@ -460,7 +470,7 @@ def _format_exception(

error_message = "\n" + error_message

exception_only[-1] = error_message + "\n"
exception_only[error_message_index] = error_message + "\n"

if is_first:
yield self._prefix
Expand Down
117 changes: 117 additions & 0 deletions tests/exceptions/output/modern/notes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@

Traceback (most recent call last):
File "tests/exceptions/source/modern/notes.py", line 13, in <module>
raise e
ValueError: invalid value
Note

Traceback (most recent call last):

> File "tests/exceptions/source/modern/notes.py", line 13, in <module>
raise e
 └ ValueError('invalid value')

ValueError: invalid value
Note

Traceback (most recent call last):
File "tests/exceptions/source/modern/notes.py", line 20, in <module>
raise e
ValueError: invalid value
Note1
Note2
Note3


Traceback (most recent call last):

> File "tests/exceptions/source/modern/notes.py", line 20, in <module>
raise e
 └ ValueError('invalid value')

ValueError: invalid value
Note1
Note2
Note3


+ Exception Group Traceback (most recent call last):
| File "tests/exceptions/source/modern/notes.py", line 27, in <module>
| raise e
| ExceptionGroup: Grouped (2 sub-exceptions)
| Note 1
| Note 2
| Note 3
+-+---------------- 1 ----------------
| ValueError: 1
+---------------- 2 ----------------
| ValueError: 2
+------------------------------------

+ Exception Group Traceback (most recent call last):
|
| > File "tests/exceptions/source/modern/notes.py", line 27, in <module>
| raise e
|  └ ExceptionGroup('Grouped', [ValueError(1), ValueError(2)])
|
| ExceptionGroup: Grouped (2 sub-exceptions)
| Note 1
| Note 2
| Note 3
+-+---------------- 1 ----------------
| ValueError: 1
+---------------- 2 ----------------
| ValueError: 2
+------------------------------------

Traceback (most recent call last):
File "tests/exceptions/source/modern/notes.py", line 32, in <module>
raise e
TabError: tab error
Note

Traceback (most recent call last):

> File "tests/exceptions/source/modern/notes.py", line 32, in <module>
raise e
 └ TabError('tab error')

TabError: tab error
Note

Traceback (most recent call last):
File "tests/exceptions/source/modern/notes.py", line 38, in <module>
raise e
File "<string>", line 1
a = 7 *
^
SyntaxError: syntax error
Note 1
Note 2

Traceback (most recent call last):

> File "tests/exceptions/source/modern/notes.py", line 38, in <module>
raise e
 └ SyntaxError('syntax error', ('<string>', 1, 8, 'a = 7 *\n', 1, 8))

File "<string>", line 1
a = 7 *
^

SyntaxError: syntax error
Note 1
Note 2

Traceback (most recent call last):
File "tests/exceptions/source/modern/notes.py", line 43, in <module>
raise e
TypeError: type error

Traceback (most recent call last):

> File "tests/exceptions/source/modern/notes.py", line 43, in <module>
raise e
 └ TypeError('type error')

TypeError: type error
43 changes: 43 additions & 0 deletions tests/exceptions/source/modern/notes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from loguru import logger
import sys


logger.remove()
logger.add(sys.stderr, format="", diagnose=False, backtrace=False, colorize=False)
logger.add(sys.stderr, format="", diagnose=True, backtrace=True, colorize=True)


with logger.catch():
e = ValueError("invalid value")
e.add_note("Note")
raise e


with logger.catch():
e = ValueError("invalid value")
e.add_note("Note1")
e.add_note("Note2\nNote3\n")
raise e


with logger.catch():
e = ExceptionGroup("Grouped", [ValueError(1), ValueError(2)])
e.add_note("Note 1\nNote 2")
e.add_note("Note 3")
raise e

with logger.catch():
e = TabError("tab error")
e.add_note("Note")
raise e

with logger.catch():
e = SyntaxError("syntax error", ("<string>", 1, 8, "a = 7 *\n", 1, 8))
e.add_note("Note 1")
e.add_note("Note 2")
raise e

with logger.catch():
e = TypeError("type error")
e.__notes__ = None
raise e
1 change: 1 addition & 0 deletions tests/test_exceptions_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ def test_exception_others(filename):
("walrus_operator", (3, 8)),
("match_statement", (3, 10)),
("exception_group_catch", (3, 11)),
("notes", (3, 11)),
("grouped_simple", (3, 11)),
("grouped_nested", (3, 11)),
("grouped_with_cause_and_context", (3, 11)),
Expand Down

0 comments on commit d70e7af

Please sign in to comment.