mirror of
https://github.com/nushell/nushell.git
synced 2025-08-10 14:28:11 +02:00
Improved labeled error from plugins (#437)
* improved labeled error from plugins * corrected span
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
use nu_protocol::{ShellError, Span, Value};
|
||||
use nu_plugin::LabeledError;
|
||||
use nu_protocol::{Span, Value};
|
||||
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
pub enum Action {
|
||||
@ -81,7 +82,7 @@ impl Inc {
|
||||
"Usage: inc field [--major|--minor|--patch]"
|
||||
}
|
||||
|
||||
pub fn inc(&self, head: Span, value: &Value) -> Result<Value, ShellError> {
|
||||
pub fn inc(&self, head: Span, value: &Value) -> Result<Value, LabeledError> {
|
||||
match value {
|
||||
Value::Int { val, span } => Ok(Value::Int {
|
||||
val: val + 1,
|
||||
@ -89,19 +90,21 @@ impl Inc {
|
||||
}),
|
||||
Value::String { val, .. } => Ok(self.apply(val)),
|
||||
x => {
|
||||
if let Ok(span) = x.span() {
|
||||
Err(ShellError::PipelineMismatch(
|
||||
"incrementable value".into(),
|
||||
head,
|
||||
span,
|
||||
))
|
||||
} else {
|
||||
Err(ShellError::LabeledError(
|
||||
"Expected incrementable value".into(),
|
||||
"incrementable value".into(),
|
||||
head,
|
||||
))
|
||||
}
|
||||
let msg = x.as_string().map_err(|e| LabeledError {
|
||||
label: "Unable to extract string".into(),
|
||||
msg: format!(
|
||||
"value cannot be converted to string {:?} - {}",
|
||||
x,
|
||||
e.to_string()
|
||||
),
|
||||
span: Some(head),
|
||||
})?;
|
||||
|
||||
Err(LabeledError {
|
||||
label: "Incorrect value".into(),
|
||||
msg,
|
||||
span: Some(head),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user