mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 10:15:41 +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:
@ -91,15 +91,15 @@ fn action(
|
||||
"crypt" => GeneralPurpose::new(&alphabet::CRYPT, NO_PAD),
|
||||
"mutf7" => GeneralPurpose::new(&alphabet::IMAP_MUTF7, NO_PAD),
|
||||
not_valid => return Value::error (
|
||||
ShellError::GenericError(
|
||||
"value is not an accepted character set".to_string(),
|
||||
format!(
|
||||
ShellError::GenericError {
|
||||
error: "value is not an accepted character set".into(),
|
||||
msg: format!(
|
||||
"{not_valid} is not a valid character-set.\nPlease use `help encode base64` to see a list of valid character sets."
|
||||
),
|
||||
Some(config_character_set.span),
|
||||
None,
|
||||
Vec::new(),
|
||||
), config_character_set.span)
|
||||
span: Some(config_character_set.span),
|
||||
help: None,
|
||||
inner: vec![],
|
||||
}, config_character_set.span)
|
||||
};
|
||||
let value_span = input.span();
|
||||
match input {
|
||||
@ -110,15 +110,15 @@ fn action(
|
||||
let mut enc_vec = vec![0; val.len() * 4 / 3 + 4];
|
||||
let bytes_written = match base64_engine.encode_slice(val, &mut enc_vec) {
|
||||
Ok(bytes_written) => bytes_written,
|
||||
Err(err) => {
|
||||
Err(e) => {
|
||||
return Value::error(
|
||||
ShellError::GenericError(
|
||||
"Error encoding data".into(),
|
||||
err.to_string(),
|
||||
Some(value_span),
|
||||
None,
|
||||
Vec::new(),
|
||||
),
|
||||
ShellError::GenericError {
|
||||
error: "Error encoding data".into(),
|
||||
msg: e.to_string(),
|
||||
span: Some(value_span),
|
||||
help: None,
|
||||
inner: vec![],
|
||||
},
|
||||
value_span,
|
||||
)
|
||||
}
|
||||
@ -157,30 +157,32 @@ fn action(
|
||||
match String::from_utf8(decoded_value) {
|
||||
Ok(string_value) => Value::string(string_value, command_span),
|
||||
Err(e) => Value::error(
|
||||
ShellError::GenericError(
|
||||
"base64 payload isn't a valid utf-8 sequence"
|
||||
.to_owned(),
|
||||
e.to_string(),
|
||||
Some(value_span),
|
||||
Some("consider using the `--binary` flag".to_owned()),
|
||||
Vec::new(),
|
||||
),
|
||||
ShellError::GenericError {
|
||||
error: "base64 payload isn't a valid utf-8 sequence"
|
||||
.into(),
|
||||
msg: e.to_string(),
|
||||
span: Some(value_span),
|
||||
help: Some(
|
||||
"consider using the `--binary` flag".to_owned(),
|
||||
),
|
||||
inner: vec![],
|
||||
},
|
||||
value_span,
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(_) => Value::error(
|
||||
ShellError::GenericError(
|
||||
"value could not be base64 decoded".to_string(),
|
||||
format!(
|
||||
ShellError::GenericError {
|
||||
error: "value could not be base64 decoded".into(),
|
||||
msg: format!(
|
||||
"invalid base64 input for character set {}",
|
||||
&config_character_set.item
|
||||
),
|
||||
Some(command_span),
|
||||
None,
|
||||
Vec::new(),
|
||||
),
|
||||
span: Some(command_span),
|
||||
help: None,
|
||||
inner: vec![],
|
||||
},
|
||||
command_span,
|
||||
),
|
||||
}
|
||||
|
@ -55,13 +55,13 @@ pub fn encode(
|
||||
// This behaviour can be enabled with -i. Otherwise, it becomes an error.
|
||||
if replacements && !ignore_errors {
|
||||
// TODO: make GenericError accept two spans (including head)
|
||||
Err(ShellError::GenericError(
|
||||
"error while encoding string".into(),
|
||||
format!("string contained characters not in {}", &encoding_name.item),
|
||||
Some(s_span),
|
||||
None,
|
||||
vec![],
|
||||
))
|
||||
Err(ShellError::GenericError {
|
||||
error: "error while encoding string".into(),
|
||||
msg: format!("string contained characters not in {}", &encoding_name.item),
|
||||
span: Some(s_span),
|
||||
help: None,
|
||||
inner: vec![],
|
||||
})
|
||||
} else {
|
||||
Ok(Value::binary(result.into_owned(), head))
|
||||
}
|
||||
@ -75,15 +75,15 @@ fn parse_encoding(span: Span, label: &str) -> Result<&'static Encoding, ShellErr
|
||||
label
|
||||
};
|
||||
match Encoding::for_label_no_replacement(label.as_bytes()) {
|
||||
None => Err(ShellError::GenericError(
|
||||
format!(
|
||||
None => Err(ShellError::GenericError{
|
||||
error: format!(
|
||||
r#"{label} is not a valid encoding"#
|
||||
),
|
||||
"invalid encoding".into(),
|
||||
Some(span),
|
||||
Some("refer to https://docs.rs/encoding_rs/latest/encoding_rs/index.html#statics for a valid list of encodings".into()),
|
||||
vec![],
|
||||
)),
|
||||
msg: "invalid encoding".into(),
|
||||
span: Some(span),
|
||||
help: Some("refer to https://docs.rs/encoding_rs/latest/encoding_rs/index.html#statics for a valid list of encodings".into()),
|
||||
inner: vec![],
|
||||
}),
|
||||
Some(encoding) => Ok(encoding),
|
||||
}
|
||||
}
|
||||
|
@ -138,14 +138,12 @@ fn operate(
|
||||
build_regex(&pattern_item, pattern_span)?
|
||||
};
|
||||
|
||||
let regex_pattern = Regex::new(&item_to_parse).map_err(|err| {
|
||||
ShellError::GenericError(
|
||||
"Error with regular expression".into(),
|
||||
err.to_string(),
|
||||
Some(pattern_span),
|
||||
None,
|
||||
Vec::new(),
|
||||
)
|
||||
let regex_pattern = Regex::new(&item_to_parse).map_err(|e| ShellError::GenericError {
|
||||
error: "Error with regular expression".into(),
|
||||
msg: e.to_string(),
|
||||
span: Some(pattern_span),
|
||||
help: None,
|
||||
inner: vec![],
|
||||
})?;
|
||||
|
||||
let columns = column_names(®ex_pattern);
|
||||
@ -164,13 +162,13 @@ fn operate(
|
||||
let captures = match c {
|
||||
Ok(c) => c,
|
||||
Err(e) => {
|
||||
return Err(ShellError::GenericError(
|
||||
"Error with regular expression captures".into(),
|
||||
e.to_string(),
|
||||
None,
|
||||
None,
|
||||
Vec::new(),
|
||||
))
|
||||
return Err(ShellError::GenericError {
|
||||
error: "Error with regular expression captures".into(),
|
||||
msg: e.to_string(),
|
||||
span: None,
|
||||
help: None,
|
||||
inner: vec![],
|
||||
})
|
||||
}
|
||||
};
|
||||
|
||||
@ -430,13 +428,13 @@ fn stream_helper(
|
||||
Ok(c) => c,
|
||||
Err(e) => {
|
||||
return Some(Value::error(
|
||||
ShellError::GenericError(
|
||||
"Error with regular expression captures".into(),
|
||||
e.to_string(),
|
||||
Some(span),
|
||||
Some(e.to_string()),
|
||||
Vec::new(),
|
||||
),
|
||||
ShellError::GenericError {
|
||||
error: "Error with regular expression captures".into(),
|
||||
msg: e.to_string(),
|
||||
span: Some(span),
|
||||
help: Some(e.to_string()),
|
||||
inner: vec![],
|
||||
},
|
||||
span,
|
||||
))
|
||||
}
|
||||
|
@ -127,14 +127,12 @@ fn split_column(
|
||||
let escaped = regex::escape(&separator.item);
|
||||
Regex::new(&escaped)
|
||||
}
|
||||
.map_err(|err| {
|
||||
ShellError::GenericError(
|
||||
"Error with regular expression".into(),
|
||||
err.to_string(),
|
||||
Some(separator.span),
|
||||
None,
|
||||
Vec::new(),
|
||||
)
|
||||
.map_err(|e| ShellError::GenericError {
|
||||
error: "Error with regular expression".into(),
|
||||
msg: e.to_string(),
|
||||
span: Some(separator.span),
|
||||
help: None,
|
||||
inner: vec![],
|
||||
})?;
|
||||
|
||||
input.flat_map(
|
||||
|
@ -161,17 +161,15 @@ impl Matcher {
|
||||
pub fn new(regex: bool, lhs: Value) -> Result<Self, ShellError> {
|
||||
if regex {
|
||||
Ok(Matcher::Regex(Regex::new(&lhs.as_string()?).map_err(
|
||||
|err| {
|
||||
ShellError::GenericError(
|
||||
"Error with regular expression".into(),
|
||||
err.to_string(),
|
||||
match lhs {
|
||||
Value::Error { .. } => None,
|
||||
_ => Some(lhs.span()),
|
||||
},
|
||||
None,
|
||||
Vec::new(),
|
||||
)
|
||||
|e| ShellError::GenericError {
|
||||
error: "Error with regular expression".into(),
|
||||
msg: e.to_string(),
|
||||
span: match lhs {
|
||||
Value::Error { .. } => None,
|
||||
_ => Some(lhs.span()),
|
||||
},
|
||||
help: None,
|
||||
inner: vec![],
|
||||
},
|
||||
)?))
|
||||
} else {
|
||||
|
@ -129,14 +129,12 @@ fn split_row(
|
||||
let escaped = regex::escape(&separator.item);
|
||||
Regex::new(&escaped)
|
||||
}
|
||||
.map_err(|err| {
|
||||
ShellError::GenericError(
|
||||
"Error with regular expression".into(),
|
||||
err.to_string(),
|
||||
Some(separator.span),
|
||||
None,
|
||||
Vec::new(),
|
||||
)
|
||||
.map_err(|e| ShellError::GenericError {
|
||||
error: "Error with regular expression".into(),
|
||||
msg: e.to_string(),
|
||||
span: Some(separator.span),
|
||||
help: None,
|
||||
inner: vec![],
|
||||
})?;
|
||||
let max_split: Option<usize> = call.get_flag(engine_state, stack, "number")?;
|
||||
input.flat_map(
|
||||
|
@ -234,7 +234,7 @@ fn str_expand(contents: &str, span: Span, value_span: Span) -> Value {
|
||||
},
|
||||
Err(e) => match e {
|
||||
bracoxide::ExpansionError::NumConversionFailed(s) => Value::error(
|
||||
ShellError::GenericError("Number Conversion Failed".to_owned(), format!("Number conversion failed at {s}."), Some(value_span), Some("Expected number, found text. Range format is `{M..N}`, where M and N are numeric values representing the starting and ending limits.".to_owned()), vec![]),
|
||||
ShellError::GenericError{error: "Number Conversion Failed".into(), msg: format!("Number conversion failed at {s}."), span: Some(value_span), help: Some("Expected number, found text. Range format is `{M..N}`, where M and N are numeric values representing the starting and ending limits.".into()), inner: vec![]},
|
||||
span,
|
||||
),
|
||||
},
|
||||
@ -243,17 +243,17 @@ fn str_expand(contents: &str, span: Span, value_span: Span) -> Value {
|
||||
Err(e) => Value::error(
|
||||
match e {
|
||||
ParsingError::NoTokens => ShellError::PipelineEmpty { dst_span: value_span },
|
||||
ParsingError::OBraExpected(s) => ShellError::GenericError("Opening Brace Expected".to_owned(), format!("Opening brace is expected at {s}."), Some(value_span), Some("In brace syntax, we use equal amount of opening (`{`) and closing (`}`). Please, take a look at the examples.".to_owned()), vec![]),
|
||||
ParsingError::CBraExpected(s) => ShellError::GenericError("Closing Brace Expected".to_owned(), format!("Closing brace is expected at {s}."), Some(value_span), Some("In brace syntax, we use equal amount of opening (`{`) and closing (`}`). Please, see the examples.".to_owned()), vec![]),
|
||||
ParsingError::RangeStartLimitExpected(s) => ShellError::GenericError("Range Start Expected".to_owned(), format!("Range start limit is missing, expected at {s}."), Some(value_span), Some("In brace syntax, Range is defined like `{X..Y}`, where X and Y are a number. X is the start, Y is the end. Please, inspect the examples for more information.".to_owned()), vec![]),
|
||||
ParsingError::RangeEndLimitExpected(s) => ShellError::GenericError("Range Start Expected".to_owned(), format!("Range start limit is missing, expected at {s}."), Some(value_span), Some("In brace syntax, Range is defined like `{X..Y}`, where X and Y are a number. X is the start, Y is the end. Please see the examples, for more information.".to_owned()), vec![]),
|
||||
ParsingError::ExpectedText(s) => ShellError::GenericError("Expected Text".to_owned(), format!("Expected text at {s}."), Some(value_span), Some("Texts are only allowed before opening brace (`{`), after closing brace (`}`), or inside `{}`. Please take a look at the examples.".to_owned()), vec![]),
|
||||
ParsingError::InvalidCommaUsage(s) => ShellError::GenericError("Invalid Comma Usage".to_owned(), format!("Found comma at {s}. Commas are only valid inside collection (`{{X,Y}}`)."), Some(value_span), Some("To escape comma use backslash `\\,`.".to_owned()), vec![]),
|
||||
ParsingError::RangeCantHaveText(s) => ShellError::GenericError("Range Can not Have Text".to_owned(), format!("Expecting, brace, number, or range operator, but found text at {s}."), Some(value_span), Some("Please use the format {M..N} for ranges in brace expansion, where M and N are numeric values representing the starting and ending limits of the sequence, respectively.".to_owned()), vec![]),
|
||||
ParsingError::ExtraRangeOperator(s) => ShellError::GenericError("Extra Range Operator".to_owned(), format!("Found additional, range operator at {s}."), Some(value_span), Some("Please, use the format `{M..N}` where M and N are numeric values representing the starting and ending limits of the range.".to_owned()), vec![]),
|
||||
ParsingError::ExtraCBra(s) => ShellError::GenericError("Extra Closing Brace".to_owned(), format!("Used extra closing brace at {s}."), Some(value_span), Some("To escape closing brace use backslash, e.g. `\\}`".to_owned()), vec![]),
|
||||
ParsingError::ExtraOBra(s) => ShellError::GenericError("Extra Opening Brace".to_owned(), format!("Used extra opening brace at {s}."), Some(value_span), Some("To escape opening brace use backslash, e.g. `\\{`".to_owned()), vec![]),
|
||||
ParsingError::NothingInBraces(s) => ShellError::GenericError("Nothing In Braces".to_owned(), format!("Nothing found inside braces at {s}."), Some(value_span), Some("Please provide valid content within the braces. Additionally, you can safely remove it, not needed.".to_owned()), vec![]),
|
||||
ParsingError::OBraExpected(s) => ShellError::GenericError{ error: "Opening Brace Expected".into(), msg: format!("Opening brace is expected at {s}."), span: Some(value_span), help: Some("In brace syntax, we use equal amount of opening (`{`) and closing (`}`). Please, take a look at the examples.".into()), inner: vec![]},
|
||||
ParsingError::CBraExpected(s) => ShellError::GenericError{ error: "Closing Brace Expected".into(), msg: format!("Closing brace is expected at {s}."), span: Some(value_span), help: Some("In brace syntax, we use equal amount of opening (`{`) and closing (`}`). Please, see the examples.".into()), inner: vec![]},
|
||||
ParsingError::RangeStartLimitExpected(s) => ShellError::GenericError{error: "Range Start Expected".into(), msg: format!("Range start limit is missing, expected at {s}."), span: Some(value_span), help: Some("In brace syntax, Range is defined like `{X..Y}`, where X and Y are a number. X is the start, Y is the end. Please, inspect the examples for more information.".into()), inner: vec![]},
|
||||
ParsingError::RangeEndLimitExpected(s) => ShellError::GenericError{ error: "Range Start Expected".into(), msg: format!("Range start limit is missing, expected at {s}."),span: Some(value_span), help: Some("In brace syntax, Range is defined like `{X..Y}`, where X and Y are a number. X is the start, Y is the end. Please see the examples, for more information.".into()), inner: vec![]},
|
||||
ParsingError::ExpectedText(s) => ShellError::GenericError { error: "Expected Text".into(), msg: format!("Expected text at {s}."), span: Some(value_span), help: Some("Texts are only allowed before opening brace (`{`), after closing brace (`}`), or inside `{}`. Please take a look at the examples.".into()), inner: vec![] },
|
||||
ParsingError::InvalidCommaUsage(s) => ShellError::GenericError { error: "Invalid Comma Usage".into(), msg: format!("Found comma at {s}. Commas are only valid inside collection (`{{X,Y}}`)."),span: Some(value_span), help: Some("To escape comma use backslash `\\,`.".into()), inner: vec![] },
|
||||
ParsingError::RangeCantHaveText(s) => ShellError::GenericError { error: "Range Can not Have Text".into(), msg: format!("Expecting, brace, number, or range operator, but found text at {s}."), span: Some(value_span), help: Some("Please use the format {M..N} for ranges in brace expansion, where M and N are numeric values representing the starting and ending limits of the sequence, respectively.".into()), inner: vec![]},
|
||||
ParsingError::ExtraRangeOperator(s) => ShellError::GenericError { error: "Extra Range Operator".into(), msg: format!("Found additional, range operator at {s}."), span: Some(value_span), help: Some("Please, use the format `{M..N}` where M and N are numeric values representing the starting and ending limits of the range.".into()), inner: vec![] },
|
||||
ParsingError::ExtraCBra(s) => ShellError::GenericError { error: "Extra Closing Brace".into(), msg: format!("Used extra closing brace at {s}."), span: Some(value_span), help: Some("To escape closing brace use backslash, e.g. `\\}`".into()), inner: vec![] },
|
||||
ParsingError::ExtraOBra(s) => ShellError::GenericError { error: "Extra Opening Brace".into(), msg: format!("Used extra opening brace at {s}."), span: Some(value_span), help: Some("To escape opening brace use backslash, e.g. `\\{`".into()), inner: vec![] },
|
||||
ParsingError::NothingInBraces(s) => ShellError::GenericError { error: "Nothing In Braces".into(), msg: format!("Nothing found inside braces at {s}."), span: Some(value_span), help: Some("Please provide valid content within the braces. Additionally, you can safely remove it, not needed.".into()), inner: vec![] },
|
||||
}
|
||||
,
|
||||
span,
|
||||
@ -267,17 +267,17 @@ fn str_expand(contents: &str, span: Span, value_span: Span) -> Value {
|
||||
),
|
||||
TokenizationError::FormatNotSupported => Value::error(
|
||||
|
||||
ShellError::GenericError(
|
||||
"Format Not Supported".to_owned(),
|
||||
"Usage of only `{` or `}`. Brace Expansion syntax, needs to have equal amount of opening (`{`) and closing (`}`)".to_owned(),
|
||||
Some(value_span),
|
||||
Some("In brace expansion syntax, it is important to have an equal number of opening (`{`) and closing (`}`) braces. Please ensure that you provide a balanced pair of braces in your brace expansion pattern.".to_owned()),
|
||||
vec![]
|
||||
),
|
||||
ShellError::GenericError {
|
||||
error: "Format Not Supported".into(),
|
||||
msg: "Usage of only `{` or `}`. Brace Expansion syntax, needs to have equal amount of opening (`{`) and closing (`}`)".into(),
|
||||
span: Some(value_span),
|
||||
help: Some("In brace expansion syntax, it is important to have an equal number of opening (`{`) and closing (`}`) braces. Please ensure that you provide a balanced pair of braces in your brace expansion pattern.".into()),
|
||||
inner: vec![]
|
||||
},
|
||||
value_span,
|
||||
),
|
||||
TokenizationError::NoBraces => Value::error(
|
||||
ShellError::GenericError("No Braces".to_owned(), "At least one `{}` brace expansion expected.".to_owned(), Some(value_span), Some("Please, examine the examples.".to_owned()), vec![]),
|
||||
ShellError::GenericError { error: "No Braces".into(), msg: "At least one `{}` brace expansion expected.".into(), span: Some(value_span), help: Some("Please, examine the examples.".into()), inner: vec![] },
|
||||
value_span,
|
||||
)
|
||||
},
|
||||
|
@ -88,13 +88,13 @@ impl Command for SubCommand {
|
||||
let to_trim = match character.as_ref() {
|
||||
Some(v) => {
|
||||
if v.item.chars().count() > 1 {
|
||||
return Err(ShellError::GenericError(
|
||||
"Trim only works with single character".into(),
|
||||
"needs single character".into(),
|
||||
Some(v.span),
|
||||
None,
|
||||
Vec::new(),
|
||||
));
|
||||
return Err(ShellError::GenericError {
|
||||
error: "Trim only works with single character".into(),
|
||||
msg: "needs single character".into(),
|
||||
span: Some(v.span),
|
||||
help: None,
|
||||
inner: vec![],
|
||||
});
|
||||
}
|
||||
v.item.chars().next()
|
||||
}
|
||||
|
Reference in New Issue
Block a user