mirror of
https://github.com/nushell/nushell.git
synced 2025-08-10 05:28:23 +02:00
Convert Shellerror::GenericError
to named fields (#11230)
# Description Replace `.to_string()` used in `GenericError` with `.into()` as `.into()` seems more popular Replace `Vec::new()` used in `GenericError` with `vec![]` as `vec![]` seems more popular (There are so, so many)
This commit is contained in:
@ -166,25 +166,25 @@ pub fn current_dir_str(engine_state: &EngineState, stack: &Stack) -> Result<Stri
|
||||
if Path::new(&cwd).is_absolute() {
|
||||
Ok(cwd)
|
||||
} else {
|
||||
Err(ShellError::GenericError(
|
||||
"Invalid current directory".to_string(),
|
||||
format!("The 'PWD' environment variable must be set to an absolute path. Found: '{cwd}'"),
|
||||
Some(pwd.span()),
|
||||
None,
|
||||
Vec::new()
|
||||
))
|
||||
Err(ShellError::GenericError {
|
||||
error: "Invalid current directory".into(),
|
||||
msg: format!("The 'PWD' environment variable must be set to an absolute path. Found: '{cwd}'"),
|
||||
span: Some(pwd.span()),
|
||||
help: None,
|
||||
inner: vec![]
|
||||
})
|
||||
}
|
||||
}
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
} else {
|
||||
Err(ShellError::GenericError(
|
||||
"Current directory not found".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(),
|
||||
))
|
||||
Err(ShellError::GenericError {
|
||||
error: "Current directory not found".into(),
|
||||
msg: "".into(),
|
||||
span: None,
|
||||
help: Some("The environment variable 'PWD' was not found. It is required to define the current directory.".into()),
|
||||
inner: vec![],
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@ -197,34 +197,34 @@ pub fn current_dir_str_const(working_set: &StateWorkingSet) -> Result<String, Sh
|
||||
if Path::new(val).is_absolute() {
|
||||
Ok(val.clone())
|
||||
} else {
|
||||
Err(ShellError::GenericError(
|
||||
"Invalid current directory".to_string(),
|
||||
format!("The 'PWD' environment variable must be set to an absolute path. Found: '{val}'"),
|
||||
Some(span),
|
||||
None,
|
||||
Vec::new()
|
||||
))
|
||||
Err(ShellError::GenericError {
|
||||
error: "Invalid current directory".into(),
|
||||
msg: format!("The 'PWD' environment variable must be set to an absolute path. Found: '{val}'"),
|
||||
span: Some(span),
|
||||
help: None,
|
||||
inner: vec![]
|
||||
})
|
||||
}
|
||||
}
|
||||
_ => Err(ShellError::GenericError(
|
||||
"PWD is not a string".to_string(),
|
||||
"".to_string(),
|
||||
None,
|
||||
Some(
|
||||
_ => Err(ShellError::GenericError {
|
||||
error: "PWD is not a string".into(),
|
||||
msg: "".into(),
|
||||
span: None,
|
||||
help: Some(
|
||||
"Cusrrent working directory environment variable 'PWD' must be a string."
|
||||
.to_string(),
|
||||
.into(),
|
||||
),
|
||||
Vec::new(),
|
||||
)),
|
||||
inner: vec![],
|
||||
}),
|
||||
}
|
||||
} else {
|
||||
Err(ShellError::GenericError(
|
||||
"Current directory not found".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(),
|
||||
))
|
||||
Err(ShellError::GenericError{
|
||||
error: "Current directory not found".into(),
|
||||
msg: "".into(),
|
||||
span: None,
|
||||
help: Some("The environment variable 'PWD' was not found. It is required to define the current directory.".into()),
|
||||
inner: vec![],
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@ -304,13 +304,13 @@ pub fn find_in_dirs_env(
|
||||
if Path::new(&cwd).is_absolute() {
|
||||
cwd
|
||||
} else {
|
||||
return Err(ShellError::GenericError(
|
||||
"Invalid current directory".to_string(),
|
||||
format!("The 'FILE_PWD' environment variable must be set to an absolute path. Found: '{cwd}'"),
|
||||
Some(pwd.span()),
|
||||
None,
|
||||
Vec::new()
|
||||
));
|
||||
return Err(ShellError::GenericError {
|
||||
error: "Invalid current directory".into(),
|
||||
msg: format!("The 'FILE_PWD' environment variable must be set to an absolute path. Found: '{cwd}'"),
|
||||
span: Some(pwd.span()),
|
||||
help: None,
|
||||
inner: vec![]
|
||||
});
|
||||
}
|
||||
}
|
||||
Err(e) => return Err(e),
|
||||
@ -434,13 +434,13 @@ 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::GenericError(
|
||||
format!("Wrong {env_path_name} environment variable value"),
|
||||
format!("{env_path_name} must be a list of strings"),
|
||||
Some(span),
|
||||
None,
|
||||
Vec::new(),
|
||||
))
|
||||
Some(ShellError::GenericError {
|
||||
error: format!("Wrong {env_path_name} environment variable value"),
|
||||
msg: format!("{env_path_name} must be a list of strings"),
|
||||
span: Some(span),
|
||||
help: None,
|
||||
inner: vec![],
|
||||
})
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -450,13 +450,13 @@ fn ensure_path(scope: &mut HashMap<String, Value>, env_path_name: &str) -> Optio
|
||||
let span = val.span();
|
||||
|
||||
error = error.or_else(|| {
|
||||
Some(ShellError::GenericError(
|
||||
format!("Wrong {env_path_name} environment variable value"),
|
||||
format!("{env_path_name} must be a list of strings"),
|
||||
Some(span),
|
||||
None,
|
||||
Vec::new(),
|
||||
))
|
||||
Some(ShellError::GenericError {
|
||||
error: format!("Wrong {env_path_name} environment variable value"),
|
||||
msg: format!("{env_path_name} must be a list of strings"),
|
||||
span: Some(span),
|
||||
help: None,
|
||||
inner: vec![],
|
||||
})
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -84,27 +84,27 @@ pub fn glob_from(
|
||||
let pattern = pattern.to_string_lossy().to_string();
|
||||
let glob_options = options.unwrap_or_default();
|
||||
|
||||
let glob = nu_glob::glob_with(&pattern, glob_options).map_err(|err| {
|
||||
nu_protocol::ShellError::GenericError(
|
||||
"Error extracting glob pattern".into(),
|
||||
err.to_string(),
|
||||
Some(span),
|
||||
None,
|
||||
Vec::new(),
|
||||
)
|
||||
let glob = nu_glob::glob_with(&pattern, glob_options).map_err(|e| {
|
||||
nu_protocol::ShellError::GenericError {
|
||||
error: "Error extracting glob pattern".into(),
|
||||
msg: e.to_string(),
|
||||
span: Some(span),
|
||||
help: None,
|
||||
inner: vec![],
|
||||
}
|
||||
})?;
|
||||
|
||||
Ok((
|
||||
prefix,
|
||||
Box::new(glob.map(move |x| match x {
|
||||
Ok(v) => Ok(v),
|
||||
Err(err) => Err(nu_protocol::ShellError::GenericError(
|
||||
"Error extracting glob pattern".into(),
|
||||
err.to_string(),
|
||||
Some(span),
|
||||
None,
|
||||
Vec::new(),
|
||||
)),
|
||||
Err(e) => Err(nu_protocol::ShellError::GenericError {
|
||||
error: "Error extracting glob pattern".into(),
|
||||
msg: e.to_string(),
|
||||
span: Some(span),
|
||||
help: None,
|
||||
inner: vec![],
|
||||
}),
|
||||
})),
|
||||
))
|
||||
}
|
||||
|
Reference in New Issue
Block a user