Skip to content

Commit 7313b06

Browse files
authored
Always show something when rendering exceptions (#166)
1 parent d54d6bb commit 7313b06

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

cleo/ui/exception_trace.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,11 @@ def ignore_files_in(self, ignore: str) -> ExceptionTrace:
252252
return self
253253

254254
def render(self, io: IO | Output, simple: bool = False) -> None:
255-
if simple:
255+
# If simple rendering wouldn't show anything useful, abandon it.
256+
simple_string = str(self._exception) if simple else ""
257+
if simple_string:
256258
io.write_line("")
257-
io.write_line(f"<error>{str(self._exception)}</error>")
259+
io.write_line(f"<error>{simple_string}</error>")
258260
else:
259261
self._render_exception(io, self._exception)
260262

tests/ui/test_exception_trace.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,3 +408,34 @@ def test_simple_render_supports_solutions():
408408
https://example2.com
409409
"""
410410
assert io.fetch_output() == expected
411+
412+
413+
def test_simple_render_aborts_if_no_message():
414+
io = BufferedIO()
415+
416+
with pytest.raises(Exception) as e:
417+
raise AssertionError
418+
419+
trace = ExceptionTrace(e.value)
420+
421+
trace.render(io, simple=True)
422+
lineno = 417
423+
424+
expected = f"""
425+
AssertionError
426+
427+
428+
429+
at {trace._get_relative_file_path(__file__)}:{lineno} in \
430+
test_simple_render_aborts_if_no_message
431+
{lineno - 4}│ def test_simple_render_aborts_if_no_message():
432+
{lineno - 3}│ io = BufferedIO()
433+
{lineno - 2}
434+
{lineno - 1}│ with pytest.raises(Exception) as e:
435+
{lineno + 0}│ raise AssertionError
436+
{lineno + 1}
437+
{lineno + 2}│ trace = ExceptionTrace(e.value)
438+
{lineno + 3}
439+
{lineno + 4}│ trace.render(io, simple=True)
440+
""" # noqa: W293
441+
assert expected == io.fetch_output()

0 commit comments

Comments
 (0)