Skip to content

Commit

Permalink
fix: improve error handling in settings validation.
Browse files Browse the repository at this point in the history
When the policy settings validation failed, the error message printed
twice in the console. This commit fix that by removing a where the error
is printed once.

It's not possible to improve more the error message because the JSON
shown in some error came from a string with the error message returned
by the settings validator. It's not possible to kwctl to easily improve
that without string manipulations.

Signed-off-by: José Guilherme Vanz <[email protected]>
  • Loading branch information
jvanz committed Jun 20, 2024
1 parent 57d6217 commit 4fbbf34
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,15 @@ async fn main() -> Result<()> {
Some("run") => {
if let Some(matches) = matches.subcommand_matches("run") {
let pull_and_run_settings = parse_pull_and_run_settings(matches).await?;
run::pull_and_run(&pull_and_run_settings).await?;
run::pull_and_run(&pull_and_run_settings)
.await
.map_err(|e| {
anyhow!(
"Error running policy {}: {}",
pull_and_run_settings.uri,
e.to_string()

Check warning on line 256 in src/main.rs

View check run for this annotation

Codecov / codecov/patch

src/main.rs#L250-L256

Added lines #L250 - L256 were not covered by tests
)
})?;
}
Ok(())
}
Expand Down
3 changes: 1 addition & 2 deletions src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,9 @@ pub(crate) async fn pull_and_run(cfg: &PullAndRunSettings) -> Result<()> {
// validate the settings given by the user
let settings_validation_response = policy_evaluator.validate_settings(&run_env.policy_settings);
if !settings_validation_response.valid {
println!("{}", serde_json::to_string(&settings_validation_response)?);
return Err(anyhow!(
"Provided settings are not valid: {:?}",
settings_validation_response.message
settings_validation_response.message.unwrap_or_default()

Check warning on line 173 in src/run.rs

View check run for this annotation

Codecov / codecov/patch

src/run.rs#L173

Added line #L173 was not covered by tests
));
}

Expand Down

0 comments on commit 4fbbf34

Please sign in to comment.