update miette and switch to GenericErrors (#5222)

This commit is contained in:
Kat Marchán
2022-04-18 05:34:10 -07:00
committed by GitHub
parent cf65f77b02
commit 1314a87cb0
141 changed files with 1569 additions and 689 deletions

View File

@ -148,19 +148,24 @@ pub fn current_dir_str(engine_state: &EngineState, stack: &Stack) -> Result<Stri
if Path::new(&cwd).is_absolute() {
Ok(cwd)
} else {
Err(ShellError::SpannedLabeledError(
Err(ShellError::GenericError(
"Invalid current directory".to_string(),
format!("The 'PWD' environment variable must be set to an absolute path. Found: '{}'", cwd),
pwd.span()?
Some(pwd.span()?),
None,
Vec::new()
))
}
}
Err(e) => Err(e),
}
} else {
Err(ShellError::LabeledError(
Err(ShellError::GenericError(
"Current directory not found".to_string(),
"The environment variable 'PWD' was not found. It is required to define the current directory.".to_string(),
"".to_string(),
None,
Some("The environment variable 'PWD' was not found. It is required to define the current directory.".to_string()),
Vec::new(),
))
}
}
@ -295,10 +300,12 @@ fn ensure_path(scope: &mut HashMap<String, Value>, env_path_name: &str) -> Optio
// Must be a list of strings
if !vals.iter().all(|v| matches!(v, Value::String { .. })) {
error = error.or_else(|| {
Some(ShellError::SpannedLabeledError(
Some(ShellError::GenericError(
format!("Wrong {} environment variable value", env_path_name),
format!("{} must be a list of strings", env_path_name),
*span,
Some(*span),
None,
Vec::new(),
))
});
}
@ -314,10 +321,12 @@ fn ensure_path(scope: &mut HashMap<String, Value>, env_path_name: &str) -> Optio
};
error = error.or_else(|| {
Some(ShellError::SpannedLabeledError(
Some(ShellError::GenericError(
format!("Wrong {} environment variable value", env_path_name),
format!("{} must be a list of strings", env_path_name),
span,
Some(span),
None,
Vec::new(),
))
});
}

View File

@ -257,6 +257,7 @@ pub fn eval_expression(
"unit value".into(),
x.get_type().to_string(),
e.span,
None,
)),
},
Expr::Range(from, next, to, operator) => {
@ -1319,20 +1320,24 @@ fn compute(size: i64, unit: Unit, span: Span) -> Value {
Unit::Day => match size.checked_mul(1000 * 1000 * 1000 * 60 * 60 * 24) {
Some(val) => Value::Duration { val, span },
None => Value::Error {
error: ShellError::SpannedLabeledError(
error: ShellError::GenericError(
"duration too large".into(),
"duration too large".into(),
span,
Some(span),
None,
Vec::new(),
),
},
},
Unit::Week => match size.checked_mul(1000 * 1000 * 1000 * 60 * 60 * 24 * 7) {
Some(val) => Value::Duration { val, span },
None => Value::Error {
error: ShellError::SpannedLabeledError(
error: ShellError::GenericError(
"duration too large".into(),
"duration too large".into(),
span,
Some(span),
None,
Vec::new(),
),
},
},

View File

@ -44,7 +44,7 @@ pub fn glob_from(
let path = if let Ok(p) = canonicalize_with(path, &cwd) {
p
} else {
return Err(ShellError::DirectoryNotFound(pattern.span));
return Err(ShellError::DirectoryNotFound(pattern.span, None));
};
if path.is_dir() {
@ -62,10 +62,12 @@ pub fn glob_from(
#[cfg(not(unix))]
let error_msg = String::from("Permission denied");
return Err(ShellError::SpannedLabeledError(
return Err(ShellError::GenericError(
"Permission denied".into(),
error_msg,
pattern.span,
Some(pattern.span),
None,
Vec::new(),
));
}
@ -82,10 +84,12 @@ pub fn glob_from(
let pattern = pattern.to_string_lossy().to_string();
let glob = nu_glob::glob(&pattern).map_err(|err| {
nu_protocol::ShellError::SpannedLabeledError(
nu_protocol::ShellError::GenericError(
"Error extracting glob pattern".into(),
err.to_string(),
span,
Some(span),
None,
Vec::new(),
)
})?;
@ -93,10 +97,12 @@ pub fn glob_from(
prefix,
Box::new(glob.map(move |x| match x {
Ok(v) => Ok(v),
Err(err) => Err(nu_protocol::ShellError::SpannedLabeledError(
Err(err) => Err(nu_protocol::ShellError::GenericError(
"Error extracting glob pattern".into(),
err.to_string(),
span,
Some(span),
None,
Vec::new(),
)),
})),
))