mirror of
https://github.com/nushell/nushell
synced 2025-01-15 14:44:14 +00:00
adds better error for failed string-to-duration conversions (#5977)
* adds better error for failed string-to-duration conversions * makes error multi-spanned, conveys literally all the information available now
This commit is contained in:
parent
e0b4ab09eb
commit
47f6d20131
2 changed files with 24 additions and 3 deletions
|
@ -136,7 +136,7 @@ fn into_duration(
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn string_to_duration(s: &str, span: Span) -> Result<i64, ShellError> {
|
fn string_to_duration(s: &str, span: Span, value_span: Span) -> Result<i64, ShellError> {
|
||||||
if let Some(expression) = parse_duration_bytes(s.as_bytes(), span) {
|
if let Some(expression) = parse_duration_bytes(s.as_bytes(), span) {
|
||||||
if let Expr::ValueWithUnit(value, unit) = expression.expr {
|
if let Expr::ValueWithUnit(value, unit) = expression.expr {
|
||||||
if let Expr::Int(x) = value.expr {
|
if let Expr::Int(x) = value.expr {
|
||||||
|
@ -155,10 +155,12 @@ fn string_to_duration(s: &str, span: Span) -> Result<i64, ShellError> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Err(ShellError::CantConvert(
|
Err(ShellError::CantConvertWithValue(
|
||||||
"duration".to_string(),
|
"duration".to_string(),
|
||||||
"string".to_string(),
|
"string".to_string(),
|
||||||
|
s.to_string(),
|
||||||
span,
|
span,
|
||||||
|
value_span,
|
||||||
Some("supported units are ns, us, ms, sec, min, hr, day, and wk".to_string()),
|
Some("supported units are ns, us, ms, sec, min, hr, day, and wk".to_string()),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
@ -166,7 +168,10 @@ fn string_to_duration(s: &str, span: Span) -> Result<i64, ShellError> {
|
||||||
fn action(input: &Value, span: Span) -> Value {
|
fn action(input: &Value, span: Span) -> Value {
|
||||||
match input {
|
match input {
|
||||||
Value::Duration { .. } => input.clone(),
|
Value::Duration { .. } => input.clone(),
|
||||||
Value::String { val, .. } => match string_to_duration(val, span) {
|
Value::String {
|
||||||
|
val,
|
||||||
|
span: value_span,
|
||||||
|
} => match string_to_duration(val, span, *value_span) {
|
||||||
Ok(val) => Value::Duration { val, span },
|
Ok(val) => Value::Duration { val, span },
|
||||||
Err(error) => Value::Error { error },
|
Err(error) => Value::Error { error },
|
||||||
},
|
},
|
||||||
|
|
|
@ -269,6 +269,22 @@ pub enum ShellError {
|
||||||
#[help] Option<String>,
|
#[help] Option<String>,
|
||||||
),
|
),
|
||||||
|
|
||||||
|
/// Failed to convert a value of one type into a different type. Includes hint for what the first value is.
|
||||||
|
///
|
||||||
|
/// ## Resolution
|
||||||
|
///
|
||||||
|
/// Not all values can be coerced this way. Check the supported type(s) and try again.
|
||||||
|
#[error("Can't convert {1} `{2}` to {0}.")]
|
||||||
|
#[diagnostic(code(nu::shell::cant_convert_with_value), url(docsrs))]
|
||||||
|
CantConvertWithValue(
|
||||||
|
String,
|
||||||
|
String,
|
||||||
|
String,
|
||||||
|
#[label("can't be converted to {0}")] Span,
|
||||||
|
#[label("this {1} value...")] Span,
|
||||||
|
#[help] Option<String>,
|
||||||
|
),
|
||||||
|
|
||||||
/// An environment variable cannot be represented as a string.
|
/// An environment variable cannot be represented as a string.
|
||||||
///
|
///
|
||||||
/// ## Resolution
|
/// ## Resolution
|
||||||
|
|
Loading…
Reference in a new issue