Add some comments on error construction.

This commit is contained in:
Jean-Christophe Amiel 2024-07-23 15:41:55 +02:00
parent cf005a13fa
commit 3d88762c59
No known key found for this signature in database
GPG Key ID: 07FF11CFD55356CC
2 changed files with 14 additions and 6 deletions

View File

@ -160,6 +160,8 @@ impl DisplaySourceError for RunnerError {
fn fixme(&self, content: &[&str]) -> StyledString {
match &self.kind {
// FIXME: this variant can not be called because message doesn't call it
// contrary to the default implementation.
RunnerErrorKind::AssertBodyDiffError { hunks, .. } => {
let mut message = StyledString::new();
for hunk in &hunks[..1] {

View File

@ -99,19 +99,25 @@ pub trait DisplaySourceError {
let error_line = self.source_info().start.line;
let error_column = self.source_info().start.column;
// The number of digits of the lines count.
let loc_max_width = max(lines.len().to_string().len(), 2);
let separator = "|";
let spaces = " ".repeat(loc_max_width);
let mut prefix = StyledString::new();
prefix.push_with(&format!("{spaces} {separator}"), Style::new().blue().bold());
// Push one-line description of the error
text.push_with(&self.description(), Style::new().bold());
text.push("\n");
// We build the prefix
let loc_max_width = max(lines.len().to_string().len(), 2);
let spaces = " ".repeat(loc_max_width);
let separator = "|";
let mut prefix = StyledString::new();
prefix.push_with(&format!("{spaces} {separator}"), Style::new().blue().bold());
// Add filename, with a left margin space for error line number.
add_filename_with_sourceinfo(&mut text, &spaces, filename, error_line, error_column);
// First line of the error message
text.append(prefix.clone());
// We can have an optional source line for context
let entry_line = entry_src_info.map(|e| e.start.line);
if let Some(entry_line) = entry_line {
add_entry_line(&mut text, &lines, error_line, entry_line, &prefix);