Allow 'error make' to make simple errors (#4881)

* Allow 'error make' to make simple errors

* Add example
This commit is contained in:
JT 2022-03-20 16:25:45 +13:00 committed by GitHub
parent d5f23ab592
commit e6a5011fdb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 5 deletions

View File

@ -65,14 +65,23 @@ impl Command for ErrorMake {
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Create a custom error for a custom command",
example: r#"def foo [x] {
vec![
Example {
description: "Create a custom error for a custom command",
example: r#"def foo [x] {
let span = (metadata $x).span;
error make {msg: "this is fishy", label: {text: "fish right here", start: $span.start, end: $span.end } }
}"#,
result: None,
}]
result: None,
},
Example {
description: "Create a simple custom error for a custom command",
example: r#"def foo [x] {
error make {msg: "this is fishy"}
}"#,
result: None,
},
]
}
}
@ -105,6 +114,9 @@ fn make_error(value: &Value) -> Option<ShellError> {
_ => None,
}
}
(Some(Value::String { val: message, .. }), None) => {
Some(ShellError::UnlabeledError(message))
}
_ => None,
}
} else {

View File

@ -315,6 +315,9 @@ Either make sure {0} is a string, or add a 'to_string' entry for it in ENV_CONVE
#[diagnostic(help("{1}"))]
LabeledError(String, String),
#[error("{0}")]
UnlabeledError(String),
#[error("{1}")]
#[diagnostic()]
OutsideSpannedLabeledError(#[source_code] String, String, String, #[label("{2}")] Span),