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

fix: correctly print multi-line errors #270

Merged
merged 2 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 5 additions & 3 deletions craft_cli/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ def ended_ok(self) -> None:
"""Finish the messaging system gracefully."""
self._stop()

def _report_error(self, error: errors.CraftError) -> None:
def _report_error(self, error: errors.CraftError) -> None: # noqa: PLR0912 (too many branches)
"""Report the different message lines from a CraftError."""
if self._mode in (EmitterMode.QUIET, EmitterMode.BRIEF, EmitterMode.VERBOSE):
use_timestamp = False
Expand All @@ -719,8 +719,10 @@ def _report_error(self, error: errors.CraftError) -> None:
use_timestamp = True
full_stream = sys.stderr

# the initial message
self._printer.show(sys.stderr, str(error), use_timestamp=use_timestamp, end_line=True)
# the initial message. Print every line individually to correctly clear
tigarmo marked this conversation as resolved.
Show resolved Hide resolved
# previous lines, if necessary.
for line in str(error).splitlines():
self._printer.show(sys.stderr, line, use_timestamp=use_timestamp, end_line=True)

if isinstance(error, errors.CraftCommandError):
stderr = error.stderr
Expand Down
9 changes: 9 additions & 0 deletions examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,15 @@ def example_30():
time.sleep(0.001)


def example_31():
"""Multiline error message."""
emit.progress("Setting up computer for build...")
time.sleep(1)
emit.progress("A long progress message")
time.sleep(6)
raise CraftError("Error 1\nError 2")


# -- end of test cases

if len(sys.argv) < 2:
Expand Down
17 changes: 17 additions & 0 deletions tests/integration/test_messages_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,23 @@ def test_error_unexpected_debugish(capsys, mode):
assert_outputs(capsys, emit, expected_err=expected, expected_log=expected)


@pytest.mark.parametrize("output_is_terminal", [True])
def test_error_multiline_brief(capsys):
emit = Emitter()
emit.init(EmitterMode.BRIEF, "testapp", GREETING)
emit.progress("A very long message detailing the current task.")
error = CraftError("Error line 1.\nError line 2.", logpath_report=False)
emit.error(error)

expected = [
Line("A very long message detailing the current task.", permanent=False),
# The error message is split on two separate lines.
Line("Error line 1.", permanent=True),
Line("Error line 2.", permanent=True),
]
assert_outputs(capsys, emit, expected_err=expected, expected_log=expected)


@pytest.mark.parametrize(
"mode",
[
Expand Down
Loading