diff --git a/crates/nu-command/src/core_commands/error_make.rs b/crates/nu-command/src/core_commands/error_make.rs index 64dd25101d..a98aaaa881 100644 --- a/crates/nu-command/src/core_commands/error_make.rs +++ b/crates/nu-command/src/core_commands/error_make.rs @@ -16,6 +16,11 @@ impl Command for ErrorMake { fn signature(&self) -> Signature { Signature::build("error make") .required("error_struct", SyntaxShape::Record, "the error to create") + .switch( + "unspanned", + "remove the origin label from the error", + Some('u'), + ) .category(Category::Core) } @@ -36,16 +41,29 @@ impl Command for ErrorMake { ) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> { let span = call.head; let arg: Value = call.req(engine_state, stack, 0)?; + let unspanned = call.has_flag("unspanned"); - Err(make_error(&arg, span).unwrap_or_else(|| { - ShellError::GenericError( - "Creating error value not supported.".into(), - "unsupported error format".into(), - Some(span), - None, - Vec::new(), - ) - })) + if unspanned { + Err(make_error(&arg, None).unwrap_or_else(|| { + ShellError::GenericError( + "Creating error value not supported.".into(), + "unsupported error format".into(), + Some(span), + None, + Vec::new(), + ) + })) + } else { + Err(make_error(&arg, Some(span)).unwrap_or_else(|| { + ShellError::GenericError( + "Creating error value not supported.".into(), + "unsupported error format".into(), + Some(span), + None, + Vec::new(), + ) + })) + } } fn examples(&self) -> Vec<Example> { @@ -69,7 +87,7 @@ impl Command for ErrorMake { } } -fn make_error(value: &Value, throw_span: Span) -> Option<ShellError> { +fn make_error(value: &Value, throw_span: Option<Span>) -> Option<ShellError> { if let Value::Record { .. } = &value { let msg = value.get_data_by_key("msg"); let label = value.get_data_by_key("label"); @@ -106,7 +124,7 @@ fn make_error(value: &Value, throw_span: Span) -> Option<ShellError> { ) => Some(ShellError::GenericError( message, label_text, - Some(throw_span), + throw_span, None, Vec::new(), )), @@ -116,7 +134,7 @@ fn make_error(value: &Value, throw_span: Span) -> Option<ShellError> { (Some(Value::String { val: message, .. }), None) => Some(ShellError::GenericError( message, "originates from here".to_string(), - Some(throw_span), + throw_span, None, Vec::new(), )), diff --git a/crates/nu-command/tests/commands/error_make.rs b/crates/nu-command/tests/commands/error_make.rs new file mode 100644 index 0000000000..c34e666f50 --- /dev/null +++ b/crates/nu-command/tests/commands/error_make.rs @@ -0,0 +1,26 @@ +use nu_test_support::{nu, pipeline}; + +#[test] +fn error_label_works() { + let actual = nu!( + cwd: ".", pipeline( + r#" + error make {msg:foo label:{text:unseen}} + "# + )); + + assert!(actual.err.contains("unseen")); + assert!(actual.err.contains("╰──")); +} + +#[test] +fn no_span_if_unspanned() { + let actual = nu!( + cwd: ".", pipeline( + r#" + error make -u {msg:foo label:{text:unseen}} + "# + )); + + assert!(!actual.err.contains("unseen")); +} diff --git a/crates/nu-command/tests/commands/mod.rs b/crates/nu-command/tests/commands/mod.rs index fd127dc32b..5977e35efe 100644 --- a/crates/nu-command/tests/commands/mod.rs +++ b/crates/nu-command/tests/commands/mod.rs @@ -15,6 +15,7 @@ mod each; mod echo; mod empty; mod enter; +mod error_make; mod every; mod find; mod first;