From aa88449f29c483fc7a799a3c3376b7ddbe6dc4d7 Mon Sep 17 00:00:00 2001 From: Stefan Holderbach Date: Fri, 20 May 2022 23:48:36 +0200 Subject: [PATCH] Refer to the span of `error make` if not given (#5599) * Refer to the span of `error make` if not given Implements #5591 Currently the span of the "throwing" `error make` Also allow to set `msg` and `label` without an additional span. * Message plus "originates from here" label --- .../src/core_commands/error_make.rs | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/crates/nu-command/src/core_commands/error_make.rs b/crates/nu-command/src/core_commands/error_make.rs index 8c514ed3f7..bfd3ec7695 100644 --- a/crates/nu-command/src/core_commands/error_make.rs +++ b/crates/nu-command/src/core_commands/error_make.rs @@ -36,7 +36,7 @@ impl Command for ErrorMake { let arg: Option = call.opt(engine_state, stack, 0)?; if let Some(arg) = arg { - Ok(make_error(&arg) + Ok(make_error(&arg, span) .map(|err| Value::Error { error: err }) .unwrap_or_else(|| Value::Error { error: ShellError::GenericError( @@ -51,7 +51,7 @@ impl Command for ErrorMake { } else { input.map( move |value| { - make_error(&value) + make_error(&value, span) .map(|err| Value::Error { error: err }) .unwrap_or_else(|| Value::Error { error: ShellError::GenericError( @@ -89,7 +89,7 @@ impl Command for ErrorMake { } } -fn make_error(value: &Value) -> Option { +fn make_error(value: &Value, throw_span: Span) -> Option { if let Value::Record { .. } = &value { let msg = value.get_data_by_key("msg"); let label = value.get_data_by_key("label"); @@ -117,13 +117,26 @@ fn make_error(value: &Value) -> Option { None, Vec::new(), )), + ( + None, + None, + Some(Value::String { + val: label_text, .. + }), + ) => Some(ShellError::GenericError( + message, + label_text, + Some(throw_span), + None, + Vec::new(), + )), _ => None, } } (Some(Value::String { val: message, .. }), None) => Some(ShellError::GenericError( message, - "".to_string(), - None, + "originates from here".to_string(), + Some(throw_span), None, Vec::new(), )),