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
This commit is contained in:
Stefan Holderbach 2022-05-20 23:48:36 +02:00 committed by GitHub
parent 06199d731b
commit aa88449f29
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -36,7 +36,7 @@ impl Command for ErrorMake {
let arg: Option<Value> = call.opt(engine_state, stack, 0)?; let arg: Option<Value> = call.opt(engine_state, stack, 0)?;
if let Some(arg) = arg { if let Some(arg) = arg {
Ok(make_error(&arg) Ok(make_error(&arg, span)
.map(|err| Value::Error { error: err }) .map(|err| Value::Error { error: err })
.unwrap_or_else(|| Value::Error { .unwrap_or_else(|| Value::Error {
error: ShellError::GenericError( error: ShellError::GenericError(
@ -51,7 +51,7 @@ impl Command for ErrorMake {
} else { } else {
input.map( input.map(
move |value| { move |value| {
make_error(&value) make_error(&value, span)
.map(|err| Value::Error { error: err }) .map(|err| Value::Error { error: err })
.unwrap_or_else(|| Value::Error { .unwrap_or_else(|| Value::Error {
error: ShellError::GenericError( error: ShellError::GenericError(
@ -89,7 +89,7 @@ impl Command for ErrorMake {
} }
} }
fn make_error(value: &Value) -> Option<ShellError> { fn make_error(value: &Value, throw_span: Span) -> Option<ShellError> {
if let Value::Record { .. } = &value { if let Value::Record { .. } = &value {
let msg = value.get_data_by_key("msg"); let msg = value.get_data_by_key("msg");
let label = value.get_data_by_key("label"); let label = value.get_data_by_key("label");
@ -117,13 +117,26 @@ fn make_error(value: &Value) -> Option<ShellError> {
None, None,
Vec::new(), Vec::new(),
)), )),
(
None,
None,
Some(Value::String {
val: label_text, ..
}),
) => Some(ShellError::GenericError(
message,
label_text,
Some(throw_span),
None,
Vec::new(),
)),
_ => None, _ => None,
} }
} }
(Some(Value::String { val: message, .. }), None) => Some(ShellError::GenericError( (Some(Value::String { val: message, .. }), None) => Some(ShellError::GenericError(
message, message,
"".to_string(), "originates from here".to_string(),
None, Some(throw_span),
None, None,
Vec::new(), Vec::new(),
)), )),