More touchups to errors

This commit is contained in:
Jonathan Turner 2019-08-06 15:03:13 +12:00
parent d8c4f73ca4
commit ae5b781159
8 changed files with 27 additions and 19 deletions

View file

@ -59,12 +59,12 @@ pub fn from_toml(args: CommandArgs) -> Result<OutputStream, ShellError> {
)), )),
} }
} }
_ => Err(ShellError::labeled_error_with_secondary( x => Err(ShellError::labeled_error_with_secondary(
"Expected a string from pipeline", "Expected a string from pipeline",
"requires string input", "requires string input",
span, span,
"value originates from here", format!("{} originates from here", x.type_name()),
a.span(), value_tag.span,
)), )),
} }
}) })

View file

@ -20,7 +20,7 @@ command! {
let full_path = PathBuf::from(cwd); let full_path = PathBuf::from(cwd);
let path_str = path.to_str().ok_or(ShellError::type_error("Path", "invalid path".simple_spanned(path.span())))?; let path_str = path.to_str().ok_or(ShellError::type_error("Path", "invalid path".tagged(path.tag())))?;
let (file_extension, contents, contents_tag, span_source) = fetch(&full_path, path_str, path.span())?; let (file_extension, contents, contents_tag, span_source) = fetch(&full_path, path_str, path.span())?;

View file

@ -30,7 +30,7 @@ pub fn split_row(args: CommandArgs) -> Result<OutputStream, ShellError> {
let mut result = VecDeque::new(); let mut result = VecDeque::new();
for s in split_result { for s in split_result {
result.push_back(ReturnSuccess::value( result.push_back(ReturnSuccess::value(
Value::Primitive(Primitive::String(s.into())).simple_spanned(v.span()), Value::Primitive(Primitive::String(s.into())).tagged(v.tag()),
)); ));
} }
result result

View file

@ -45,10 +45,12 @@ pub fn to_csv(args: CommandArgs) -> Result<OutputStream, ShellError> {
Ok(x) => ReturnSuccess::value( Ok(x) => ReturnSuccess::value(
Value::Primitive(Primitive::String(x)).simple_spanned(name_span), Value::Primitive(Primitive::String(x)).simple_spanned(name_span),
), ),
Err(_) => Err(ShellError::labeled_error( _ => Err(ShellError::labeled_error_with_secondary(
"Can not convert to CSV string", "Expected an object with CSV-compatible structure from pipeline",
"can not convert piped data to CSV string", "requires CSV-compatible input",
name_span, name_span,
format!("{} originates from here", a.item.type_name()),
a.span(),
)), )),
}) })
.to_output_stream()) .to_output_stream())

View file

@ -51,10 +51,12 @@ pub fn to_json(args: CommandArgs) -> Result<OutputStream, ShellError> {
Ok(x) => ReturnSuccess::value( Ok(x) => ReturnSuccess::value(
Value::Primitive(Primitive::String(x)).simple_spanned(name_span), Value::Primitive(Primitive::String(x)).simple_spanned(name_span),
), ),
Err(_) => Err(ShellError::labeled_error( _ => Err(ShellError::labeled_error_with_secondary(
"Can not convert to JSON string", "Expected an object with JSON-compatible structure from pipeline",
"can not convert piped data to JSON string", "requires JSON-compatible input",
name_span, name_span,
format!("{} originates from here", a.item.type_name()),
a.span(),
)), )),
}, },
) )

View file

@ -45,10 +45,12 @@ pub fn to_toml(args: CommandArgs) -> Result<OutputStream, ShellError> {
Value::Primitive(Primitive::String(val)).simple_spanned(name_span), Value::Primitive(Primitive::String(val)).simple_spanned(name_span),
) )
} }
_ => Err(ShellError::labeled_error_with_secondary(
Err(err) => Err(ShellError::type_error( "Expected an object with TOML-compatible structure from pipeline",
"Can not convert to a TOML string", "requires TOML-compatible input",
format!("{:?} - {:?}", a.type_name(), err).simple_spanned(name_span), name_span,
format!("{} originates from here", a.item.type_name()),
a.span(),
)), )),
}) })
.to_output_stream()) .to_output_stream())

View file

@ -49,10 +49,12 @@ pub fn to_yaml(args: CommandArgs) -> Result<OutputStream, ShellError> {
Ok(x) => ReturnSuccess::value( Ok(x) => ReturnSuccess::value(
Value::Primitive(Primitive::String(x)).simple_spanned(name_span), Value::Primitive(Primitive::String(x)).simple_spanned(name_span),
), ),
Err(_) => Err(ShellError::labeled_error( _ => Err(ShellError::labeled_error_with_secondary(
"Can not convert to YAML string", "Expected an object with YAML-compatible structure from pipeline",
"can not convert piped data to YAML string", "requires YAML-compatible input",
name_span, name_span,
format!("{} originates from here", a.item.type_name()),
a.span(),
)), )),
}, },
) )

View file

@ -21,7 +21,7 @@ pub trait ExtractType: Sized {
impl<T: ExtractType> ExtractType for Tagged<T> { impl<T: ExtractType> ExtractType for Tagged<T> {
fn extract(value: &Tagged<Value>) -> Result<Tagged<T>, ShellError> { fn extract(value: &Tagged<Value>) -> Result<Tagged<T>, ShellError> {
Ok(T::extract(value)?.simple_spanned(value.span())) Ok(T::extract(value)?.tagged(value.tag()))
} }
fn check(value: &'value Tagged<Value>) -> Result<&'value Tagged<Value>, ShellError> { fn check(value: &'value Tagged<Value>) -> Result<&'value Tagged<Value>, ShellError> {