mirror of
https://github.com/nushell/nushell.git
synced 2025-08-10 12:18:07 +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:
@ -101,13 +101,13 @@ impl Command for Complete {
|
||||
|
||||
Ok(Value::record(record, call.head).into_pipeline_data())
|
||||
}
|
||||
_ => Err(ShellError::GenericError(
|
||||
"Complete only works with external streams".to_string(),
|
||||
"complete only works on external streams".to_string(),
|
||||
Some(call.head),
|
||||
None,
|
||||
Vec::new(),
|
||||
)),
|
||||
_ => Err(ShellError::GenericError {
|
||||
error: "Complete only works with external streams".into(),
|
||||
msg: "complete only works on external streams".into(),
|
||||
span: Some(call.head),
|
||||
help: None,
|
||||
inner: vec![],
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -81,13 +81,13 @@ fn exec_impl(mut command: std::process::Command, span: Span) -> Result<PipelineD
|
||||
|
||||
let error = command.exec();
|
||||
|
||||
Err(ShellError::GenericError(
|
||||
"Error on exec".into(),
|
||||
error.to_string(),
|
||||
Some(span),
|
||||
None,
|
||||
Vec::new(),
|
||||
))
|
||||
Err(ShellError::GenericError {
|
||||
error: "Error on exec".into(),
|
||||
msg: error.to_string(),
|
||||
span: Some(span),
|
||||
help: None,
|
||||
inner: vec![],
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
|
@ -53,11 +53,13 @@ impl Command for NuCheck {
|
||||
let mut working_set = StateWorkingSet::new(engine_state);
|
||||
|
||||
if is_all && is_module {
|
||||
return Err(ShellError::GenericError(
|
||||
"Detected command flags conflict".to_string(),
|
||||
"You cannot have both `--all` and `--as-module` on the same command line, please refer to `nu-check --help` for more details".to_string(),
|
||||
Some(call.head),
|
||||
None, vec![]));
|
||||
return Err(ShellError::GenericError {
|
||||
error: "Detected command flags conflict".into(),
|
||||
msg: "You cannot have both `--all` and `--as-module` on the same command line, please refer to `nu-check --help` for more details".into(),
|
||||
span: Some(call.head),
|
||||
help: None,
|
||||
inner: vec![]
|
||||
});
|
||||
}
|
||||
|
||||
let span = input.span().unwrap_or(call.head);
|
||||
@ -130,13 +132,13 @@ impl Command for NuCheck {
|
||||
|
||||
let ext: Vec<_> = path_str.rsplitn(2, '.').collect();
|
||||
if ext[0] != "nu" {
|
||||
return Err(ShellError::GenericError(
|
||||
"Cannot parse input".to_string(),
|
||||
"File extension must be the type of .nu".to_string(),
|
||||
Some(call.head),
|
||||
None,
|
||||
Vec::new(),
|
||||
));
|
||||
return Err(ShellError::GenericError {
|
||||
error: "Cannot parse input".into(),
|
||||
msg: "File extension must be the type of .nu".into(),
|
||||
span: Some(call.head),
|
||||
help: None,
|
||||
inner: vec![],
|
||||
});
|
||||
}
|
||||
|
||||
// Change currently parsed directory
|
||||
@ -163,13 +165,13 @@ impl Command for NuCheck {
|
||||
|
||||
result
|
||||
} else {
|
||||
Err(ShellError::GenericError(
|
||||
"Failed to execute command".to_string(),
|
||||
"Please run 'nu-check --help' for more details".to_string(),
|
||||
Some(call.head),
|
||||
None,
|
||||
Vec::new(),
|
||||
))
|
||||
Err(ShellError::GenericError {
|
||||
error: "Failed to execute command".into(),
|
||||
msg: "Please run 'nu-check --help' for more details".into(),
|
||||
span: Some(call.head),
|
||||
help: None,
|
||||
inner: vec![],
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -241,13 +243,13 @@ fn heuristic_parse(
|
||||
Ok(v) => Ok(v),
|
||||
Err(_) => {
|
||||
if is_debug {
|
||||
Err(ShellError::GenericError(
|
||||
"Failed to parse content,tried both script and module".to_string(),
|
||||
"syntax error".to_string(),
|
||||
Some(span),
|
||||
Some("Run `nu-check --help` for more details".to_string()),
|
||||
Vec::new(),
|
||||
))
|
||||
Err(ShellError::GenericError {
|
||||
error: "Failed to parse content,tried both script and module".into(),
|
||||
msg: "syntax error".into(),
|
||||
span: Some(span),
|
||||
help: Some("Run `nu-check --help` for more details".into()),
|
||||
inner: vec![],
|
||||
})
|
||||
} else {
|
||||
Ok(PipelineData::Value(Value::bool(false, span), None))
|
||||
}
|
||||
@ -285,14 +287,14 @@ fn heuristic_parse_file(
|
||||
Ok(v) => Ok(v),
|
||||
Err(_) => {
|
||||
if is_debug {
|
||||
Err(ShellError::GenericError(
|
||||
"Failed to parse content,tried both script and module"
|
||||
.to_string(),
|
||||
"syntax error".to_string(),
|
||||
Some(call.head),
|
||||
Some("Run `nu-check --help` for more details".to_string()),
|
||||
Vec::new(),
|
||||
))
|
||||
Err(ShellError::GenericError {
|
||||
error: "Failed to parse content,tried both script and module"
|
||||
.into(),
|
||||
msg: "syntax error".into(),
|
||||
span: Some(call.head),
|
||||
help: Some("Run `nu-check --help` for more details".into()),
|
||||
inner: vec![],
|
||||
})
|
||||
} else {
|
||||
Ok(PipelineData::Value(Value::bool(false, call.head), None))
|
||||
}
|
||||
@ -334,13 +336,13 @@ fn parse_module(
|
||||
.first()
|
||||
.expect("Unable to parse content as module")
|
||||
);
|
||||
Err(ShellError::GenericError(
|
||||
"Failed to parse content".to_string(),
|
||||
Err(ShellError::GenericError {
|
||||
error: "Failed to parse content".into(),
|
||||
msg,
|
||||
Some(span),
|
||||
Some("If the content is intended to be a script, please try to remove `--as-module` flag ".to_string()),
|
||||
Vec::new(),
|
||||
))
|
||||
span: Some(span),
|
||||
help: Some("If the content is intended to be a script, please try to remove `--as-module` flag ".into()),
|
||||
inner: vec![],
|
||||
})
|
||||
} else {
|
||||
Ok(PipelineData::Value(Value::bool(false, new_span), None))
|
||||
}
|
||||
@ -367,13 +369,13 @@ fn parse_script(
|
||||
.expect("Unable to parse content")
|
||||
);
|
||||
if is_debug {
|
||||
Err(ShellError::GenericError(
|
||||
"Failed to parse content".to_string(),
|
||||
Err(ShellError::GenericError {
|
||||
error: "Failed to parse content".into(),
|
||||
msg,
|
||||
Some(span),
|
||||
Some("If the content is intended to be a module, please consider flag of `--as-module` ".to_string()),
|
||||
Vec::new(),
|
||||
))
|
||||
span: Some(span),
|
||||
help: Some("If the content is intended to be a module, please consider flag of `--as-module` ".into()),
|
||||
inner: vec![],
|
||||
})
|
||||
} else {
|
||||
Ok(PipelineData::Value(Value::bool(false, span), None))
|
||||
}
|
||||
|
@ -126,15 +126,16 @@ fn run_ps(engine_state: &EngineState, call: &Call) -> Result<PipelineData, Shell
|
||||
not(target_os = "ios")
|
||||
))]
|
||||
{
|
||||
let proc_stat = proc.curr_proc.stat().map_err(|e| {
|
||||
ShellError::GenericError(
|
||||
"Error getting process stat".into(),
|
||||
e.to_string(),
|
||||
Some(Span::unknown()),
|
||||
None,
|
||||
Vec::new(),
|
||||
)
|
||||
})?;
|
||||
let proc_stat = proc
|
||||
.curr_proc
|
||||
.stat()
|
||||
.map_err(|e| ShellError::GenericError {
|
||||
error: "Error getting process stat".into(),
|
||||
msg: e.to_string(),
|
||||
span: Some(Span::unknown()),
|
||||
help: None,
|
||||
inner: vec![],
|
||||
})?;
|
||||
// If we can't get the start time, just use the current time
|
||||
let proc_start = proc_stat
|
||||
.starttime()
|
||||
|
@ -130,13 +130,13 @@ fn registry_query(
|
||||
)
|
||||
.into_pipeline_data())
|
||||
}
|
||||
Err(_) => Err(ShellError::GenericError(
|
||||
"Unable to find registry key/value".to_string(),
|
||||
format!("Registry value: {} was not found", value.item),
|
||||
Some(value.span),
|
||||
None,
|
||||
Vec::new(),
|
||||
)),
|
||||
Err(_) => Err(ShellError::GenericError {
|
||||
error: "Unable to find registry key/value".into(),
|
||||
msg: format!("Registry value: {} was not found", value.item),
|
||||
span: Some(value.span),
|
||||
help: None,
|
||||
inner: vec![],
|
||||
}),
|
||||
}
|
||||
}
|
||||
None => Ok(Value::nothing(call_span).into_pipeline_data()),
|
||||
@ -153,13 +153,13 @@ fn get_reg_hive(call: &Call) -> Result<RegKey, ShellError> {
|
||||
.filter(|flag| call.has_flag(flag))
|
||||
.collect();
|
||||
if flags.len() > 1 {
|
||||
return Err(ShellError::GenericError(
|
||||
"Only one registry key can be specified".into(),
|
||||
"Only one registry key can be specified".into(),
|
||||
Some(call.head),
|
||||
None,
|
||||
Vec::new(),
|
||||
));
|
||||
return Err(ShellError::GenericError {
|
||||
error: "Only one registry key can be specified".into(),
|
||||
msg: "Only one registry key can be specified".into(),
|
||||
span: Some(call.head),
|
||||
help: None,
|
||||
inner: vec![],
|
||||
});
|
||||
}
|
||||
let hive = flags.first().copied().unwrap_or("hkcu");
|
||||
let hkey = match hive {
|
||||
|
@ -601,16 +601,16 @@ impl ExternalCommand {
|
||||
}
|
||||
process
|
||||
} else {
|
||||
return Err(ShellError::GenericError(
|
||||
"Current directory not found".to_string(),
|
||||
"did not find PWD environment variable".to_string(),
|
||||
Some(span),
|
||||
Some(concat!(
|
||||
return Err(ShellError::GenericError{
|
||||
error: "Current directory not found".into(),
|
||||
msg: "did not find PWD environment variable".into(),
|
||||
span: Some(span),
|
||||
help: Some(concat!(
|
||||
"The environment variable 'PWD' was not found. ",
|
||||
"It is required to define the current directory when running an external command."
|
||||
).to_string()),
|
||||
Vec::new(),
|
||||
));
|
||||
).into()),
|
||||
inner:Vec::new(),
|
||||
});
|
||||
};
|
||||
|
||||
process.envs(&self.env_vars);
|
||||
|
Reference in New Issue
Block a user