add context to Expand errors
// Licensed under the MIT License.
use crate::sha256::digest_file_blocking;
-use anyhow::Result;
+use anyhow::{Context, Result};
use onefuzz_telemetry::{InstanceTelemetryKey, MicrosoftTelemetryKey};
use std::path::{Path, PathBuf};
use std::{collections::HashMap, hash::Hash};
@@ -314,7 +314,13 @@ impl<'a> Expand<'a> {
) -> Result<String> {
match ev {
ExpandedValue::Path(v) => {
- let path = String::from(dunce::canonicalize(v)?.to_string_lossy());
+ let path = String::from(
+ dunce::canonicalize(&v)
+ .with_context(|| {
+ format!("unable to canonicalize path during extension: {}", v)
+ })?
+ .to_string_lossy(),
+ );
arg = arg.replace(fmtstr, &path);
Ok(arg)
}
@@ -348,7 +354,11 @@ impl<'a> Expand<'a> {
arg.contains(fmtstr),
self.values.get(&placeholder.get_string()),
) {
- (true, Some(ev)) => arg = self.replace_value(fmtstr, arg, ev)?,
+ (true, Some(ev)) => {
+ arg = self
+ .replace_value(fmtstr, arg.clone(), ev)
+ .with_context(|| format!("replace_value failed: {} {}", fmtstr, arg))?
+ }
(true, None) => bail!("missing argument {}", fmtstr),
(false, _) => (),
}
@@ -359,7 +369,9 @@ impl<'a> Expand<'a> {
pub fn evaluate<T: AsRef<str>>(&self, args: &[T]) -> Result<Vec<String>> {
let mut result = Vec::new();
for arg in args {
- let arg = self.evaluate_value(arg)?;
+ let arg = self
+ .evaluate_value(arg)
+ .with_context(|| format!("evaluating argument failed: {}", arg.as_ref()))?;
result.push(arg);
}
Ok(result)
