mirror of
https://github.com/nushell/nushell
synced 2024-12-27 21:43:09 +00:00
add unspanned
flag to error make, add tests (#6017)
* add `unspanned` flag to error make, add tests * fmt
This commit is contained in:
parent
a5470b2362
commit
a54f9719e5
3 changed files with 57 additions and 12 deletions
|
@ -16,6 +16,11 @@ impl Command for ErrorMake {
|
||||||
fn signature(&self) -> Signature {
|
fn signature(&self) -> Signature {
|
||||||
Signature::build("error make")
|
Signature::build("error make")
|
||||||
.required("error_struct", SyntaxShape::Record, "the error to create")
|
.required("error_struct", SyntaxShape::Record, "the error to create")
|
||||||
|
.switch(
|
||||||
|
"unspanned",
|
||||||
|
"remove the origin label from the error",
|
||||||
|
Some('u'),
|
||||||
|
)
|
||||||
.category(Category::Core)
|
.category(Category::Core)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,8 +41,10 @@ impl Command for ErrorMake {
|
||||||
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||||
let span = call.head;
|
let span = call.head;
|
||||||
let arg: Value = call.req(engine_state, stack, 0)?;
|
let arg: Value = call.req(engine_state, stack, 0)?;
|
||||||
|
let unspanned = call.has_flag("unspanned");
|
||||||
|
|
||||||
Err(make_error(&arg, span).unwrap_or_else(|| {
|
if unspanned {
|
||||||
|
Err(make_error(&arg, None).unwrap_or_else(|| {
|
||||||
ShellError::GenericError(
|
ShellError::GenericError(
|
||||||
"Creating error value not supported.".into(),
|
"Creating error value not supported.".into(),
|
||||||
"unsupported error format".into(),
|
"unsupported error format".into(),
|
||||||
|
@ -46,6 +53,17 @@ impl Command for ErrorMake {
|
||||||
Vec::new(),
|
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> {
|
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 {
|
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");
|
||||||
|
@ -106,7 +124,7 @@ fn make_error(value: &Value, throw_span: Span) -> Option<ShellError> {
|
||||||
) => Some(ShellError::GenericError(
|
) => Some(ShellError::GenericError(
|
||||||
message,
|
message,
|
||||||
label_text,
|
label_text,
|
||||||
Some(throw_span),
|
throw_span,
|
||||||
None,
|
None,
|
||||||
Vec::new(),
|
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(
|
(Some(Value::String { val: message, .. }), None) => Some(ShellError::GenericError(
|
||||||
message,
|
message,
|
||||||
"originates from here".to_string(),
|
"originates from here".to_string(),
|
||||||
Some(throw_span),
|
throw_span,
|
||||||
None,
|
None,
|
||||||
Vec::new(),
|
Vec::new(),
|
||||||
)),
|
)),
|
||||||
|
|
26
crates/nu-command/tests/commands/error_make.rs
Normal file
26
crates/nu-command/tests/commands/error_make.rs
Normal file
|
@ -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"));
|
||||||
|
}
|
|
@ -15,6 +15,7 @@ mod each;
|
||||||
mod echo;
|
mod echo;
|
||||||
mod empty;
|
mod empty;
|
||||||
mod enter;
|
mod enter;
|
||||||
|
mod error_make;
|
||||||
mod every;
|
mod every;
|
||||||
mod find;
|
mod find;
|
||||||
mod first;
|
mod first;
|
||||||
|
|
Loading…
Reference in a new issue