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 show something when rendering exceptions #166

Merged
merged 1 commit into from Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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()