🐛 Fix path command error messages (#2261). (#2276)

This commit is contained in:
Warren Seine 2020-07-30 20:51:14 +02:00 committed by GitHub
parent 3282a509a9
commit aca7f71737
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 18 additions and 9 deletions

View File

@ -27,8 +27,9 @@ impl WholeStreamCommand for PathBasename {
args: CommandArgs, args: CommandArgs,
registry: &CommandRegistry, registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> { ) -> Result<OutputStream, ShellError> {
let tag = args.call_info.name_tag.clone();
let (DefaultArguments { rest }, input) = args.process(&registry).await?; let (DefaultArguments { rest }, input) = args.process(&registry).await?;
operate(input, rest, &action).await operate(input, rest, &action, tag.span).await
} }
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {

View File

@ -26,8 +26,9 @@ impl WholeStreamCommand for PathExists {
args: CommandArgs, args: CommandArgs,
registry: &CommandRegistry, registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> { ) -> Result<OutputStream, ShellError> {
let tag = args.call_info.name_tag.clone();
let (DefaultArguments { rest }, input) = args.process(&registry).await?; let (DefaultArguments { rest }, input) = args.process(&registry).await?;
operate(input, rest, &action).await operate(input, rest, &action, tag.span).await
} }
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {

View File

@ -26,8 +26,9 @@ impl WholeStreamCommand for PathExpand {
args: CommandArgs, args: CommandArgs,
registry: &CommandRegistry, registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> { ) -> Result<OutputStream, ShellError> {
let tag = args.call_info.name_tag.clone();
let (DefaultArguments { rest }, input) = args.process(&registry).await?; let (DefaultArguments { rest }, input) = args.process(&registry).await?;
operate(input, rest, &action).await operate(input, rest, &action, tag.span).await
} }
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {

View File

@ -27,8 +27,9 @@ impl WholeStreamCommand for PathExtension {
args: CommandArgs, args: CommandArgs,
registry: &CommandRegistry, registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> { ) -> Result<OutputStream, ShellError> {
let tag = args.call_info.name_tag.clone();
let (DefaultArguments { rest }, input) = args.process(&registry).await?; let (DefaultArguments { rest }, input) = args.process(&registry).await?;
operate(input, rest, &action).await operate(input, rest, &action, tag.span).await
} }
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {

View File

@ -8,6 +8,7 @@ mod r#type;
use crate::prelude::*; use crate::prelude::*;
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::{ColumnPath, Primitive, ReturnSuccess, ShellTypeName, UntaggedValue, Value}; use nu_protocol::{ColumnPath, Primitive, ReturnSuccess, ShellTypeName, UntaggedValue, Value};
use nu_source::Span;
use std::path::Path; use std::path::Path;
pub use basename::PathBasename; pub use basename::PathBasename;
@ -22,7 +23,7 @@ struct DefaultArguments {
rest: Vec<ColumnPath>, rest: Vec<ColumnPath>,
} }
fn handle_value<F>(action: &F, v: &Value) -> Result<Value, ShellError> fn handle_value<F>(action: &F, v: &Value, span: Span) -> Result<Value, ShellError>
where where
F: Fn(&Path) -> UntaggedValue + Send + 'static, F: Fn(&Path) -> UntaggedValue + Send + 'static,
{ {
@ -32,9 +33,11 @@ where
| UntaggedValue::Primitive(Primitive::Line(s)) => action(s.as_ref()).into_value(v.tag()), | UntaggedValue::Primitive(Primitive::Line(s)) => action(s.as_ref()).into_value(v.tag()),
other => { other => {
let got = format!("got {}", other.type_name()); let got = format!("got {}", other.type_name());
return Err(ShellError::labeled_error( return Err(ShellError::labeled_error_with_secondary(
"value is not string or path", "value is not string or path",
got, got,
span,
"originates from here".to_string(),
v.tag().span, v.tag().span,
)); ));
} }
@ -46,6 +49,7 @@ async fn operate<F>(
input: crate::InputStream, input: crate::InputStream,
paths: Vec<ColumnPath>, paths: Vec<ColumnPath>,
action: &'static F, action: &'static F,
span: Span,
) -> Result<OutputStream, ShellError> ) -> Result<OutputStream, ShellError>
where where
F: Fn(&Path) -> UntaggedValue + Send + Sync + 'static, F: Fn(&Path) -> UntaggedValue + Send + Sync + 'static,
@ -53,14 +57,14 @@ where
Ok(input Ok(input
.map(move |v| { .map(move |v| {
if paths.is_empty() { if paths.is_empty() {
ReturnSuccess::value(handle_value(&action, &v)?) ReturnSuccess::value(handle_value(&action, &v, span)?)
} else { } else {
let mut ret = v; let mut ret = v;
for path in &paths { for path in &paths {
ret = ret.swap_data_by_column_path( ret = ret.swap_data_by_column_path(
path, path,
Box::new(move |old| handle_value(&action, &old)), Box::new(move |old| handle_value(&action, &old, span)),
)?; )?;
} }

View File

@ -27,8 +27,9 @@ impl WholeStreamCommand for PathType {
args: CommandArgs, args: CommandArgs,
registry: &CommandRegistry, registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> { ) -> Result<OutputStream, ShellError> {
let tag = args.call_info.name_tag.clone();
let (DefaultArguments { rest }, input) = args.process(&registry).await?; let (DefaultArguments { rest }, input) = args.process(&registry).await?;
operate(input, rest, &action).await operate(input, rest, &action, tag.span).await
} }
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {