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:
Eric Hodel
2023-12-06 15:40:03 -08:00
committed by GitHub
parent b03f1efac4
commit a95a4505ef
160 changed files with 2975 additions and 3228 deletions

View File

@ -87,13 +87,13 @@ impl Command for PluginDeclaration {
let mut child = plugin_cmd.spawn().map_err(|err| {
let decl = engine_state.get_decl(call.decl_id);
ShellError::GenericError(
format!("Unable to spawn plugin for {}", decl.name()),
format!("{err}"),
Some(call.head),
None,
Vec::new(),
)
ShellError::GenericError {
error: format!("Unable to spawn plugin for {}", decl.name()),
msg: format!("{err}"),
span: Some(call.head),
help: None,
inner: vec![],
}
})?;
let input = input.into_value(call.head);
@ -109,16 +109,16 @@ impl Command for PluginDeclaration {
}
_ => {
let custom_value_name = val.value_string();
return Err(ShellError::GenericError(
format!(
return Err(ShellError::GenericError {
error: format!(
"Plugin {} can not handle the custom value {}",
self.name, custom_value_name
),
format!("custom value {custom_value_name}"),
Some(span),
None,
Vec::new(),
));
msg: format!("custom value {custom_value_name}"),
span: Some(span),
help: None,
inner: vec![],
});
}
}
}
@ -145,13 +145,13 @@ impl Command for PluginDeclaration {
};
let response = call_plugin(&mut child, plugin_call, &encoding, call.head).map_err(|err| {
let decl = engine_state.get_decl(call.decl_id);
ShellError::GenericError(
format!("Unable to decode call for {}", decl.name()),
err.to_string(),
Some(call.head),
None,
Vec::new(),
)
ShellError::GenericError {
error: format!("Unable to decode call for {}", decl.name()),
msg: err.to_string(),
span: Some(call.head),
help: None,
inner: vec![],
}
});
let pipeline_data = match response {
@ -172,13 +172,13 @@ impl Command for PluginDeclaration {
None,
)),
Ok(PluginResponse::Error(err)) => Err(err.into()),
Ok(PluginResponse::Signature(..)) => Err(ShellError::GenericError(
"Plugin missing value".into(),
"Received a signature from plugin instead of value".into(),
Some(call.head),
None,
Vec::new(),
)),
Ok(PluginResponse::Signature(..)) => Err(ShellError::GenericError {
error: "Plugin missing value".into(),
msg: "Received a signature from plugin instead of value".into(),
span: Some(call.head),
help: None,
inner: vec![],
}),
Err(err) => Err(err),
};

View File

@ -111,13 +111,13 @@ pub(crate) fn call_plugin(
encoding.decode_response(&mut buf_read)
} else {
Err(ShellError::GenericError(
"Error with stdout reader".into(),
"no stdout reader".into(),
Some(span),
None,
Vec::new(),
))
Err(ShellError::GenericError {
error: "Error with stdout reader".into(),
msg: "no stdout reader".into(),
span: Some(span),
help: None,
inner: vec![],
})
}
}

View File

@ -48,16 +48,20 @@ pub struct LabeledError {
impl From<LabeledError> for ShellError {
fn from(error: LabeledError) -> Self {
match error.span {
Some(span) => {
ShellError::GenericError(error.label, error.msg, Some(span), None, Vec::new())
}
None => ShellError::GenericError(
error.label,
"".to_string(),
None,
Some(error.msg),
Vec::new(),
),
Some(span) => ShellError::GenericError {
error: error.label,
msg: error.msg,
span: Some(span),
help: None,
inner: vec![],
},
None => ShellError::GenericError {
error: error.label,
msg: "".into(),
span: None,
help: Some(error.msg),
inner: vec![],
},
}
}
}
@ -65,9 +69,12 @@ impl From<LabeledError> for ShellError {
impl From<ShellError> for LabeledError {
fn from(error: ShellError) -> Self {
match error {
ShellError::GenericError(label, msg, span, _help, _related) => {
LabeledError { label, msg, span }
}
ShellError::GenericError {
error: label,
msg,
span,
..
} => LabeledError { label, msg, span },
ShellError::CantConvert {
to_type: expected,
from_type: input,

View File

@ -47,17 +47,15 @@ impl CustomValue for PluginCustomValue {
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
let mut plugin_cmd = create_command(&self.filename, self.shell.as_deref());
let mut child = plugin_cmd.spawn().map_err(|err| {
ShellError::GenericError(
format!(
"Unable to spawn plugin for {} to get base value",
self.source
),
format!("{err}"),
Some(span),
None,
Vec::new(),
)
let mut child = plugin_cmd.spawn().map_err(|err| ShellError::GenericError {
error: format!(
"Unable to spawn plugin for {} to get base value",
self.source
),
msg: format!("{err}"),
span: Some(span),
help: None,
inner: vec![],
})?;
let plugin_call = PluginCall::CollapseCustomValue(PluginData {
@ -77,35 +75,35 @@ impl CustomValue for PluginCustomValue {
};
let response = call_plugin(&mut child, plugin_call, &encoding, span).map_err(|err| {
ShellError::GenericError(
format!(
ShellError::GenericError {
error: format!(
"Unable to decode call for {} to get base value",
self.source
),
format!("{err}"),
Some(span),
None,
Vec::new(),
)
msg: format!("{err}"),
span: Some(span),
help: None,
inner: vec![],
}
});
let value = match response {
Ok(PluginResponse::Value(value)) => Ok(*value),
Ok(PluginResponse::PluginData(..)) => Err(ShellError::GenericError(
"Plugin misbehaving".into(),
"Plugin returned custom data as a response to a collapse call".into(),
Some(span),
None,
Vec::new(),
)),
Ok(PluginResponse::PluginData(..)) => Err(ShellError::GenericError {
error: "Plugin misbehaving".into(),
msg: "Plugin returned custom data as a response to a collapse call".into(),
span: Some(span),
help: None,
inner: vec![],
}),
Ok(PluginResponse::Error(err)) => Err(err.into()),
Ok(PluginResponse::Signature(..)) => Err(ShellError::GenericError(
"Plugin missing value".into(),
"Received a signature from plugin instead of value".into(),
Some(span),
None,
Vec::new(),
)),
Ok(PluginResponse::Signature(..)) => Err(ShellError::GenericError {
error: "Plugin missing value".into(),
msg: "Received a signature from plugin instead of value".into(),
span: Some(span),
help: None,
inner: vec![],
}),
Err(err) => Err(err),
};