Skip to content

Commit

Permalink
cli: highlight error source headers as well
Browse files Browse the repository at this point in the history
Highlighting "{n}: " will help to follow error sources containing multi-line
messages. I'm going to make revset/template alias errors be formatted as plain
error chain.
  • Loading branch information
yuja committed Mar 26, 2024
1 parent 0481e67 commit 05c9520
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
23 changes: 14 additions & 9 deletions cli/src/command_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,15 +543,20 @@ fn print_error_sources(ui: &Ui, source: Option<&dyn error::Error>) -> io::Result
let Some(err) = source else {
return Ok(());
};
if err.source().is_none() {
writeln!(ui.stderr(), "Caused by: {err}")?;
} else {
writeln!(ui.stderr(), "Caused by:")?;
for (i, err) in iter::successors(Some(err), |err| err.source()).enumerate() {
writeln!(ui.stderr(), "{n}: {err}", n = i + 1)?;
}
}
Ok(())
ui.stderr_formatter()
.with_label("error_source", |formatter| {
if err.source().is_none() {
write!(formatter.labeled("heading"), "Caused by: ")?;
writeln!(formatter, "{err}")?;
} else {
writeln!(formatter.labeled("heading"), "Caused by:")?;
for (i, err) in iter::successors(Some(err), |err| err.source()).enumerate() {
write!(formatter.labeled("heading"), "{}: ", i + 1)?;
writeln!(formatter, "{err}")?;
}
}
Ok(())
})
}

fn handle_clap_error(ui: &mut Ui, err: &clap::Error, hints: &[String]) -> io::Result<ExitCode> {
Expand Down
2 changes: 2 additions & 0 deletions cli/src/config/colors.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
[colors]
"error" = { fg = "default", bold = true }
"error_source" = { fg = "default" }
"warning" = { fg = "default", bold = true }
"hint" = { fg = "default" }
"error heading" = { fg = "red", bold = true }
"error_source heading" = { bold = true }
"warning heading" = { fg = "yellow", bold = true }
"hint heading" = { fg = "cyan", bold = true }

Expand Down
9 changes: 8 additions & 1 deletion cli/tests/test_global_opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,13 @@ fn test_color_ui_messages() {
Error: There is no jj repo in "."
"###);

// error source
let stderr = test_env.jj_cmd_failure(&repo_path, &["log", ".."]);
insta::assert_snapshot!(stderr, @r###"
Error: Path ".." is not in the repo "."
Caused by: Invalid component ".." in repo-relative path "../"
"###);

// warning
let (_stdout, stderr) = test_env.jj_cmd_ok(&repo_path, &["log", "@"]);
insta::assert_snapshot!(stderr, @r###"
Expand All @@ -418,7 +425,7 @@ fn test_color_ui_messages() {
],
);
insta::assert_snapshot!(stdout, @r###"
[38;5;4m8bb159bc30a9859930e567eb9238a7c43ee6744d[39m
[38;5;4m167f90e7600a50f85c4f909b53eaf546faa82879[39m
<Error: No commit available> (elided revisions)
0000000000000000000000000000000000000000
"###);
Expand Down

0 comments on commit 05c9520

Please sign in to comment.