From f6d1c9860c8dbe29ed98b31a597e83bb5f3bb3ee Mon Sep 17 00:00:00 2001 From: Helio Frota <00hf11@gmail.com> Date: Fri, 13 Dec 2024 10:42:13 -0300 Subject: [PATCH] chore: Better error message for end users Related to #1093 --- modules/importer/src/runner/csaf/mod.rs | 23 +++++++++++++++++------ modules/importer/src/runner/sbom/mod.rs | 23 +++++++++++++++++------ 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/modules/importer/src/runner/csaf/mod.rs b/modules/importer/src/runner/csaf/mod.rs index addc29fde..ce660c731 100644 --- a/modules/importer/src/runner/csaf/mod.rs +++ b/modules/importer/src/runner/csaf/mod.rs @@ -103,12 +103,23 @@ impl super::ImportRunner { .await // if the walker fails, we record the outcome as part of the report, but skip any // further processing, like storing the marker - .map_err(|err| ScannerError::Normal { - err: err.into(), - output: RunOutput { - report: report.lock().clone().build(), - continuation: None, - }, + .map_err(|err| { + // Formatted error for user (UI) + let err_string = err.to_string(); + // Checks if the error message contains a custom prefix like `Visitor error:` + let formatted_err = if let Some(index) = err_string.find(':') { + format!("Error: {}", &err_string[index + 1..].trim_start()) + } else { + format!("Error: {}", err_string) + }; + + ScannerError::Normal { + err: anyhow::Error::msg(formatted_err), + output: RunOutput { + report: report.lock().clone().build(), + continuation: None, + }, + } })?; Ok(match Arc::try_unwrap(report) { diff --git a/modules/importer/src/runner/sbom/mod.rs b/modules/importer/src/runner/sbom/mod.rs index 4ed9109ae..38a5f7ec5 100644 --- a/modules/importer/src/runner/sbom/mod.rs +++ b/modules/importer/src/runner/sbom/mod.rs @@ -107,12 +107,23 @@ impl super::ImportRunner { .await // if the walker fails, we record the outcome as part of the report, but skip any // further processing, like storing the marker - .map_err(|err| ScannerError::Normal { - err: err.into(), - output: RunOutput { - report: report.lock().clone().build(), - continuation: None, - }, + .map_err(|err| { + // Formatted error for user (UI) + let err_string = err.to_string(); + // Checks if the error message contains a custom prefix like `Visitor error:` + let formatted_err = if let Some(index) = err_string.find(':') { + format!("Error: {}", &err_string[index + 1..].trim_start()) + } else { + format!("Error: {}", err_string) + }; + + ScannerError::Normal { + err: anyhow::Error::msg(formatted_err), + output: RunOutput { + report: report.lock().clone().build(), + continuation: None, + }, + } })?; Ok(match Arc::try_unwrap(report) {