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

@ -21,28 +21,28 @@ pub(super) fn process_string_enum<T, E>(
*config_point = format;
}
Err(err) => {
errors.push(ShellError::GenericError(
"Error while applying config changes".into(),
format!(
errors.push(ShellError::GenericError {
error: "Error while applying config changes".into(),
msg: format!(
"unrecognized $env.config.{} option '{v}'",
config_path.join(".")
),
Some(span),
Some(err.to_string()),
vec![],
));
span: Some(span),
help: Some(err.to_string()),
inner: vec![],
});
// Reconstruct
*value = config_point.reconstruct_value(span);
}
}
} else {
errors.push(ShellError::GenericError(
"Error while applying config changes".into(),
format!("$env.config.{} should be a string", config_path.join(".")),
Some(span),
Some("This value will be ignored.".into()),
vec![],
));
errors.push(ShellError::GenericError {
error: "Error while applying config changes".into(),
msg: format!("$env.config.{} should be a string", config_path.join(".")),
span: Some(span),
help: Some("This value will be ignored.".into()),
inner: vec![],
});
// Reconstruct
*value = config_point.reconstruct_value(span);
}
@ -56,13 +56,13 @@ pub(super) fn process_bool_config(
if let Ok(b) = value.as_bool() {
*config_point = b;
} else {
errors.push(ShellError::GenericError(
"Error while applying config changes".into(),
"should be a bool".to_string(),
Some(value.span()),
Some("This value will be ignored.".into()),
vec![],
));
errors.push(ShellError::GenericError {
error: "Error while applying config changes".into(),
msg: "should be a bool".to_string(),
span: Some(value.span()),
help: Some("This value will be ignored.".into()),
inner: vec![],
});
// Reconstruct
*value = Value::bool(*config_point, value.span());
}
@ -76,13 +76,13 @@ pub(super) fn process_int_config(
if let Ok(b) = value.as_int() {
*config_point = b;
} else {
errors.push(ShellError::GenericError(
"Error while applying config changes".into(),
"should be an int".to_string(),
Some(value.span()),
Some("This value will be ignored.".into()),
vec![],
));
errors.push(ShellError::GenericError {
error: "Error while applying config changes".into(),
msg: "should be an int".into(),
span: Some(value.span()),
help: Some("This value will be ignored.".into()),
inner: vec![],
});
// Reconstruct
*value = Value::int(*config_point, value.span());
}
@ -92,26 +92,26 @@ pub(super) fn report_invalid_key(keys: &[&str], span: Span, errors: &mut Vec<She
// Because Value::Record discards all of the spans of its
// column names (by storing them as Strings), the key name cannot be provided
// as a value, even in key errors.
errors.push(ShellError::GenericError(
"Error while applying config changes".into(),
format!(
errors.push(ShellError::GenericError {
error: "Error while applying config changes".into(),
msg: format!(
"$env.config.{} is an unknown config setting",
keys.join(".")
),
Some(span),
Some("This value will not appear in your $env.config record.".into()),
vec![],
));
span: Some(span),
help: Some("This value will not appear in your $env.config record.".into()),
inner: vec![],
});
}
pub(super) fn report_invalid_value(msg: &str, span: Span, errors: &mut Vec<ShellError>) {
errors.push(ShellError::GenericError(
"Error while applying config changes".into(),
msg.into(),
Some(span),
Some("This value will be ignored.".into()),
vec![],
));
errors.push(ShellError::GenericError {
error: "Error while applying config changes".into(),
msg: msg.into(),
span: Some(span),
help: Some("This value will be ignored.".into()),
inner: vec![],
});
}
pub(super) fn create_map(value: &Value) -> Result<HashMap<String, Value>, ShellError> {

View File

@ -705,13 +705,13 @@ impl Value {
} else {
return (
config,
Some(ShellError::GenericError(
"Error while applying config changes".into(),
"$env.config is not a record".into(),
Some(self.span()),
None,
vec![],
)),
Some(ShellError::GenericError {
error: "Error while applying config changes".into(),
msg: "$env.config is not a record".into(),
span: Some(self.span()),
help: None,
inner: vec![],
}),
);
}
@ -719,14 +719,13 @@ impl Value {
(
config,
if !errors.is_empty() {
Some(ShellError::GenericError(
"Config record contains invalid values or unknown settings".into(),
// Without a span, this second string is ignored.
"".into(),
None,
None,
errors,
))
Some(ShellError::GenericError {
error: "Config record contains invalid values or unknown settings".into(),
msg: "".into(),
span: None,
help: None,
inner: errors,
})
} else {
None
},

View File

@ -206,14 +206,12 @@ pub(super) fn try_parse_trim_strategy(
value: &Value,
errors: &mut Vec<ShellError>,
) -> Result<TrimStrategy, ShellError> {
let map = value.as_record().map_err(|e| {
ShellError::GenericError(
"Error while applying config changes".into(),
"$env.config.table.trim is not a record".into(),
Some(value.span()),
Some("Please consult the documentation for configuring Nushell.".into()),
vec![e],
)
let map = value.as_record().map_err(|e| ShellError::GenericError {
error: "Error while applying config changes".into(),
msg: "$env.config.table.trim is not a record".into(),
span: Some(value.span()),
help: Some("Please consult the documentation for configuring Nushell.".into()),
inner: vec![e],
})?;
let mut methodology = match map.get("methodology") {
@ -222,13 +220,13 @@ pub(super) fn try_parse_trim_strategy(
None => return Ok(TrimStrategy::default()),
},
None => {
errors.push(ShellError::GenericError(
"Error while applying config changes".into(),
"$env.config.table.trim.methodology was not provided".into(),
Some(value.span()),
Some("Please consult the documentation for configuring Nushell.".into()),
vec![],
));
errors.push(ShellError::GenericError {
error: "Error while applying config changes".into(),
msg: "$env.config.table.trim.methodology was not provided".into(),
span: Some(value.span()),
help: Some("Please consult the documentation for configuring Nushell.".into()),
inner: vec![],
});
return Ok(TrimStrategy::default());
}
};
@ -239,13 +237,15 @@ pub(super) fn try_parse_trim_strategy(
if let Ok(b) = value.as_bool() {
*try_to_keep_words = b;
} else {
errors.push(ShellError::GenericError(
"Error while applying config changes".into(),
"$env.config.table.trim.wrapping_try_keep_words is not a bool".into(),
Some(value.span()),
Some("Please consult the documentation for configuring Nushell.".into()),
vec![],
));
errors.push(ShellError::GenericError {
error: "Error while applying config changes".into(),
msg: "$env.config.table.trim.wrapping_try_keep_words is not a bool".into(),
span: Some(value.span()),
help: Some(
"Please consult the documentation for configuring Nushell.".into(),
),
inner: vec![],
});
}
}
}
@ -254,13 +254,15 @@ pub(super) fn try_parse_trim_strategy(
if let Ok(v) = value.as_string() {
*suffix = Some(v);
} else {
errors.push(ShellError::GenericError(
"Error while applying config changes".into(),
"$env.config.table.trim.truncating_suffix is not a string".into(),
Some(value.span()),
Some("Please consult the documentation for configuring Nushell.".into()),
vec![],
));
errors.push(ShellError::GenericError {
error: "Error while applying config changes".into(),
msg: "$env.config.table.trim.truncating_suffix is not a string".into(),
span: Some(value.span()),
help: Some(
"Please consult the documentation for configuring Nushell.".into(),
),
inner: vec![],
});
}
}
}

View File

@ -526,14 +526,12 @@ impl EngineState {
})
})
.and_then(|_| {
plugin_file.flush().map_err(|err| {
ShellError::GenericError(
"Error flushing plugin file".to_string(),
format! {"{err}"},
None,
None,
Vec::new(),
)
plugin_file.flush().map_err(|err| ShellError::GenericError {
error: "Error flushing plugin file".into(),
msg: format! {"{err}"},
span: None,
help: None,
inner: vec![],
})
})
})

View File

@ -82,13 +82,13 @@ impl Stack {
}
if var_id == NU_VARIABLE_ID || var_id == ENV_VARIABLE_ID {
return Err(ShellError::GenericError(
"Built-in variables `$env` and `$nu` have no metadata".into(),
"no metadata available".into(),
Some(span),
None,
Vec::new(),
));
return Err(ShellError::GenericError {
error: "Built-in variables `$env` and `$nu` have no metadata".into(),
msg: "no metadata available".into(),
span: Some(span),
help: None,
inner: vec![],
});
}
Err(ShellError::VariableNotFoundAtRuntime { span })

View File

@ -666,28 +666,28 @@ impl PipelineData {
match (&val.to, &val.from) {
(Value::Float { val, .. }, _) | (_, Value::Float { val, .. }) => {
if *val == f64::INFINITY || *val == f64::NEG_INFINITY {
return Err(ShellError::GenericError(
"Cannot create range".into(),
"Infinity is not allowed when converting to json".into(),
Some(span),
Some("Consider removing infinity".into()),
vec![],
));
return Err(ShellError::GenericError {
error: "Cannot create range".into(),
msg: "Infinity is not allowed when converting to json".into(),
span: Some(span),
help: Some("Consider removing infinity".into()),
inner: vec![],
});
}
}
(Value::Int { val, .. }, _) => {
if *val == i64::MAX || *val == i64::MIN {
return Err(ShellError::GenericError(
"Cannot create range".into(),
"Unbounded ranges are not allowed when converting to json"
return Err(ShellError::GenericError {
error: "Cannot create range".into(),
msg: "Unbounded ranges are not allowed when converting to json"
.into(),
Some(span),
Some(
span: Some(span),
help: Some(
"Consider using ranges with valid start and end point."
.into(),
),
vec![],
));
inner: vec![],
});
}
}
_ => (),

View File

@ -1070,15 +1070,18 @@ pub enum ShellError {
},
/// This is a generic error type used for different situations.
#[error("{0}")]
#[error("{error}")]
#[diagnostic()]
GenericError(
String,
String,
#[label("{1}")] Option<Span>,
#[help] Option<String>,
#[related] Vec<ShellError>,
),
GenericError {
error: String,
msg: String,
#[label("{msg}")]
span: Option<Span>,
#[help]
help: Option<String>,
#[related]
inner: Vec<ShellError>,
},
/// This is a generic error type used for different situations.
#[error("{1}")]

View File

@ -696,13 +696,13 @@ impl Command for BlockCommand {
_call: &Call,
_input: PipelineData,
) -> Result<crate::PipelineData, crate::ShellError> {
Err(ShellError::GenericError(
"Internal error: can't run custom command with 'run', use block_id".to_string(),
"".to_string(),
None,
None,
Vec::new(),
))
Err(ShellError::GenericError {
error: "Internal error: can't run custom command with 'run', use block_id".into(),
msg: "".into(),
span: None,
help: None,
inner: vec![],
})
}
fn get_block_id(&self) -> Option<BlockId> {

View File

@ -67,43 +67,43 @@ impl Unit {
Unit::Second => Ok(Value::duration(size * 1000 * 1000 * 1000, span)),
Unit::Minute => match size.checked_mul(1000 * 1000 * 1000 * 60) {
Some(val) => Ok(Value::duration(val, span)),
None => Err(ShellError::GenericError(
"duration too large".into(),
"duration too large".into(),
Some(span),
None,
Vec::new(),
)),
None => Err(ShellError::GenericError {
error: "duration too large".into(),
msg: "duration too large".into(),
span: Some(span),
help: None,
inner: vec![],
}),
},
Unit::Hour => match size.checked_mul(1000 * 1000 * 1000 * 60 * 60) {
Some(val) => Ok(Value::duration(val, span)),
None => Err(ShellError::GenericError(
"duration too large".into(),
"duration too large".into(),
Some(span),
None,
Vec::new(),
)),
None => Err(ShellError::GenericError {
error: "duration too large".into(),
msg: "duration too large".into(),
span: Some(span),
help: None,
inner: vec![],
}),
},
Unit::Day => match size.checked_mul(1000 * 1000 * 1000 * 60 * 60 * 24) {
Some(val) => Ok(Value::duration(val, span)),
None => Err(ShellError::GenericError(
"duration too large".into(),
"duration too large".into(),
Some(span),
None,
Vec::new(),
)),
None => Err(ShellError::GenericError {
error: "duration too large".into(),
msg: "duration too large".into(),
span: Some(span),
help: None,
inner: vec![],
}),
},
Unit::Week => match size.checked_mul(1000 * 1000 * 1000 * 60 * 60 * 24 * 7) {
Some(val) => Ok(Value::duration(val, span)),
None => Err(ShellError::GenericError(
"duration too large".into(),
"duration too large".into(),
Some(span),
None,
Vec::new(),
)),
None => Err(ShellError::GenericError {
error: "duration too large".into(),
msg: "duration too large".into(),
span: Some(span),
help: None,
inner: vec![],
}),
},
}
}