On short error format, append primary span label to message

The `error-format=short` output only displays the path, error code and
main error message all in the same line. We now add the primary span label
as well after the error message, to provide more context.
This commit is contained in:
Esteban Küber 2024-06-21 19:22:59 +00:00
parent e57f3090ae
commit 1bbaf6eb2f
7 changed files with 34 additions and 14 deletions

View file

@ -1346,10 +1346,11 @@ impl HumanEmitter {
buffer.append(0, ": ", header_style);
label_width += 2;
}
let mut line = 0;
for (text, _) in msgs.iter() {
let text = self.translate_message(text, args).map_err(Report::new).unwrap();
// Account for newlines to align output to its label.
for (line, text) in normalize_whitespace(&text).lines().enumerate() {
for text in normalize_whitespace(&text).lines() {
buffer.append(
line,
&format!(
@ -1359,6 +1360,25 @@ impl HumanEmitter {
),
header_style,
);
line += 1;
}
}
if self.short_message {
let labels = msp
.span_labels()
.into_iter()
.filter_map(|label| match label.label {
Some(msg) if label.is_primary => {
let text = self.translate_message(&msg, args).ok()?;
if !text.trim().is_empty() { Some(text.to_string()) } else { None }
}
_ => None,
})
.collect::<Vec<_>>()
.join(", ");
if !labels.is_empty() {
buffer.append(line, ": ", Style::NoStyle);
buffer.append(line, &labels, Style::NoStyle);
}
}
}