Skip to content

Commit

Permalink
Always show something when rendering exceptions (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimbleby committed Jul 12, 2022
1 parent d54d6bb commit 7313b06
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
6 changes: 4 additions & 2 deletions cleo/ui/exception_trace.py
Expand Up @@ -252,9 +252,11 @@ def ignore_files_in(self, ignore: str) -> ExceptionTrace:
return self

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

Expand Down
31 changes: 31 additions & 0 deletions tests/ui/test_exception_trace.py
Expand Up @@ -408,3 +408,34 @@ def test_simple_render_supports_solutions():
https://example2.com
"""
assert io.fetch_output() == expected


def test_simple_render_aborts_if_no_message():
io = BufferedIO()

with pytest.raises(Exception) as e:
raise AssertionError

trace = ExceptionTrace(e.value)

trace.render(io, simple=True)
lineno = 417

expected = f"""
AssertionError
at {trace._get_relative_file_path(__file__)}:{lineno} in \
test_simple_render_aborts_if_no_message
{lineno - 4}│ def test_simple_render_aborts_if_no_message():
{lineno - 3}│ io = BufferedIO()
{lineno - 2}
{lineno - 1}│ with pytest.raises(Exception) as e:
{lineno + 0}│ raise AssertionError
{lineno + 1}
{lineno + 2}│ trace = ExceptionTrace(e.value)
{lineno + 3}
{lineno + 4}│ trace.render(io, simple=True)
""" # noqa: W293
assert expected == io.fetch_output()

0 comments on commit 7313b06

Please sign in to comment.