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

feat: improve error reporting on invalid rules file path #140

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
Loading