diff --git a/crates/nu-cmd-lang/src/core_commands/error_make.rs b/crates/nu-cmd-lang/src/core_commands/error_make.rs index 25efbecf49..2c4f8c6bf2 100644 --- a/crates/nu-cmd-lang/src/core_commands/error_make.rs +++ b/crates/nu-cmd-lang/src/core_commands/error_make.rs @@ -134,13 +134,25 @@ fn make_error(value: &Value, throw_span: Option) -> Option { Some(Value::String { val: label_text, .. }), - ) => Some(ShellError::GenericError( - message, - label_text, - Some(Span::new(start as usize, end as usize)), - None, - Vec::new(), - )), + ) => { + if start > end { + Some(ShellError::GenericError( + "invalid error format.".into(), + "`$.label.start` should be smaller than `$.label.end`".into(), + label_span, + Some(format!("{} > {}", start, end)), + Vec::new(), + )) + } else { + Some(ShellError::GenericError( + message, + label_text, + Some(Span::new(start as usize, end as usize)), + None, + Vec::new(), + )) + } + } ( None, None, diff --git a/crates/nu-command/tests/commands/error_make.rs b/crates/nu-command/tests/commands/error_make.rs index c34e666f50..40a1bb209e 100644 --- a/crates/nu-command/tests/commands/error_make.rs +++ b/crates/nu-command/tests/commands/error_make.rs @@ -24,3 +24,18 @@ fn no_span_if_unspanned() { assert!(!actual.err.contains("unseen")); } + +#[test] +fn error_start_bigger_than_end_should_fail() { + let actual = nu!( + cwd: ".", pipeline( + r#" + error make {msg: foo label: {text: bar start 456 end 123}} + "# + )); + + assert!(!actual.err.contains("invalid error format")); + assert!(!actual + .err + .contains("`$.label.start` should be smaller than `$.label.end`")); +}