Skip to content

Commit

Permalink
feat: improve error reporting on invalid rules file path
Browse files Browse the repository at this point in the history
The error reported when the rules file given to boreal-cli is missing
was not very good, since it did not indicate the path to the file.
This is now improved.
  • Loading branch information
vthib committed Apr 22, 2024
1 parent e97d345 commit c0b69c8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
4 changes: 3 additions & 1 deletion boreal-cli/tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ fn test_invalid_path() {
.arg("input")
.assert()
.stdout("")
.stderr(predicate::str::contains("IO error"))
.stderr(predicate::str::contains(
"Cannot read rules file do_not_exist: ",
))
.failure();

// Invalid path to input
Expand Down
19 changes: 16 additions & 3 deletions boreal/src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,10 @@ impl Compiler {
) -> Result<(), AddRuleError> {
let contents = std::fs::read_to_string(path).map_err(|error| AddRuleError {
path: Some(path.to_path_buf()),
kind: AddRuleErrorKind::IO(error),
kind: AddRuleErrorKind::IO {
path: path.to_path_buf(),
error,
},
})?;
self.add_rules_str_inner(&contents, namespace, Some(path), status)
}
Expand Down Expand Up @@ -600,7 +603,13 @@ enum AddRuleErrorKind {
/// - when using the [`Compiler::add_rules_file`] or [`Compiler::add_rules_file_in_namespace`]
/// and failing to read from the provided path.
/// - On `include` clauses.
IO(std::io::Error),
IO {
/// Path to the file.
path: PathBuf,

/// IO error on this path.
error: std::io::Error,
},

InvalidInclude {
/// Path in the include clause that is invalid.
Expand Down Expand Up @@ -655,7 +664,11 @@ impl AddRuleError {
impl AddRuleErrorKind {
fn to_diagnostic(&self) -> Diagnostic<()> {
match self {
Self::IO(error) => Diagnostic::error().with_message(format!("IO error: {error}")),
Self::IO { path, error } => Diagnostic::error().with_message(format!(
"Cannot read rules file {}: {}",
path.display(),
error
)),
Self::InvalidInclude { path, span, error } => Diagnostic::error()
.with_message(format!("cannot include `{}`: {error}", path.display()))
.with_labels(vec![Label::primary((), span.clone())]),
Expand Down
4 changes: 2 additions & 2 deletions boreal/tests/it/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ fn test_add_rules_file_err() {
let err = compiler.add_rules_file(path).unwrap_err();
assert!(err
.to_short_description(path, "")
.starts_with("error: IO error: "));
.starts_with("error: Cannot read rules file non_existing: "));

let err = compiler
.add_rules_file_in_namespace(path, "ns")
.unwrap_err();
assert!(err
.to_short_description(path, "")
.starts_with("error: IO error: "));
.starts_with("error: Cannot read rules file non_existing: "));
}

// An import is reused in the same namespace
Expand Down

0 comments on commit c0b69c8

Please sign in to comment.