Bubble errors even if pipeline isn't used (#2080)

This commit is contained in:
Jonathan Turner 2020-06-30 05:39:11 +12:00 committed by GitHub
parent bcddeb3c1f
commit ed10aafa6f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 64 additions and 37 deletions

View File

@ -121,7 +121,7 @@ pub async fn autoview(context: RunnableContext) -> Result<OutputStream, ShellErr
if let Some(table) = table { if let Some(table) = table {
let command_args = create_default_command_args(&context).with_input(stream); let command_args = create_default_command_args(&context).with_input(stream);
let result = table.run(command_args, &context.registry).await; let result = table.run(command_args, &context.registry).await?;
result.collect::<Vec<_>>().await; result.collect::<Vec<_>>().await;
} }
} }
@ -138,7 +138,7 @@ pub async fn autoview(context: RunnableContext) -> Result<OutputStream, ShellErr
); );
let command_args = let command_args =
create_default_command_args(&context).with_input(stream); create_default_command_args(&context).with_input(stream);
let result = text.run(command_args, &context.registry).await; let result = text.run(command_args, &context.registry).await?;
result.collect::<Vec<_>>().await; result.collect::<Vec<_>>().await;
} else { } else {
out!("{}", s); out!("{}", s);
@ -161,7 +161,7 @@ pub async fn autoview(context: RunnableContext) -> Result<OutputStream, ShellErr
); );
let command_args = let command_args =
create_default_command_args(&context).with_input(stream); create_default_command_args(&context).with_input(stream);
let result = text.run(command_args, &context.registry).await; let result = text.run(command_args, &context.registry).await?;
result.collect::<Vec<_>>().await; result.collect::<Vec<_>>().await;
} else { } else {
out!("{}\n", s); out!("{}\n", s);
@ -236,7 +236,7 @@ pub async fn autoview(context: RunnableContext) -> Result<OutputStream, ShellErr
stream.push_back(x); stream.push_back(x);
let command_args = let command_args =
create_default_command_args(&context).with_input(stream); create_default_command_args(&context).with_input(stream);
let result = binary.run(command_args, &context.registry).await; let result = binary.run(command_args, &context.registry).await?;
result.collect::<Vec<_>>().await; result.collect::<Vec<_>>().await;
} else { } else {
use pretty_hex::*; use pretty_hex::*;
@ -298,7 +298,7 @@ pub async fn autoview(context: RunnableContext) -> Result<OutputStream, ShellErr
stream.push_back(x); stream.push_back(x);
let command_args = let command_args =
create_default_command_args(&context).with_input(stream); create_default_command_args(&context).with_input(stream);
let result = table.run(command_args, &context.registry).await; let result = table.run(command_args, &context.registry).await?;
result.collect::<Vec<_>>().await; result.collect::<Vec<_>>().await;
} else { } else {
out!("{:?}", item); out!("{:?}", item);

View File

@ -37,7 +37,7 @@ pub(crate) async fn run_internal_command(
&scope, &scope,
objects, objects,
) )
.await .await?
}; };
let head = Arc::new(command.args.head.clone()); let head = Arc::new(command.args.head.clone());
@ -89,12 +89,15 @@ pub(crate) async fn run_internal_command(
scope: (&*scope).clone(), scope: (&*scope).clone(),
}, },
}; };
let mut result = converter let result = converter
.run( .run(
new_args.with_input(vec![tagged_contents]), new_args.with_input(vec![tagged_contents]),
&context.registry, &context.registry,
) )
.await; .await;
match result {
Ok(mut result) => {
let result_vec: Vec<Result<ReturnSuccess, ShellError>> = let result_vec: Vec<Result<ReturnSuccess, ShellError>> =
result.drain_vec().await; result.drain_vec().await;
@ -109,10 +112,13 @@ pub(crate) async fn run_internal_command(
output.push(Ok(l)); output.push(Ok(l));
} }
} }
Ok(ReturnSuccess::Value(Value { value, .. })) => { Ok(ReturnSuccess::Value(Value {
output.push(Ok( value,
value.into_value(contents_tag.clone()) ..
)); })) => {
output
.push(Ok(value
.into_value(contents_tag.clone())));
} }
Err(e) => output.push(Err(e)), Err(e) => output.push(Err(e)),
_ => {} _ => {}
@ -120,6 +126,12 @@ pub(crate) async fn run_internal_command(
} }
futures::stream::iter(output).to_input_stream() futures::stream::iter(output).to_input_stream()
}
Err(e) => {
context.add_error(e);
InputStream::empty()
}
}
} else { } else {
InputStream::one(tagged_contents) InputStream::one(tagged_contents)
} }

View File

@ -343,18 +343,19 @@ impl Command {
self.0.usage() self.0.usage()
} }
pub async fn run(&self, args: CommandArgs, registry: &CommandRegistry) -> OutputStream { pub async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
if args.call_info.switch_present("help") { if args.call_info.switch_present("help") {
let cl = self.0.clone(); let cl = self.0.clone();
let registry = registry.clone(); let registry = registry.clone();
OutputStream::one(Ok(ReturnSuccess::Value( Ok(OutputStream::one(Ok(ReturnSuccess::Value(
UntaggedValue::string(get_help(&*cl, &registry)).into_value(Tag::unknown()), UntaggedValue::string(get_help(&*cl, &registry)).into_value(Tag::unknown()),
))) ))))
} else { } else {
match self.0.run(args, registry).await { self.0.run(args, registry).await
Ok(stream) => stream,
Err(err) => OutputStream::one(Err(err)),
}
} }
} }

View File

@ -158,7 +158,7 @@ async fn enter(
}; };
let mut result = converter let mut result = converter
.run(new_args.with_input(vec![tagged_contents]), &registry) .run(new_args.with_input(vec![tagged_contents]), &registry)
.await; .await?;
let result_vec: Vec<Result<ReturnSuccess, ShellError>> = let result_vec: Vec<Result<ReturnSuccess, ShellError>> =
result.drain_vec().await; result.drain_vec().await;

View File

@ -232,7 +232,7 @@ async fn save(
scope, scope,
}, },
}; };
let mut result = converter.run(new_args.with_input(input), &registry).await; let mut result = converter.run(new_args.with_input(input), &registry).await?;
let result_vec: Vec<Result<ReturnSuccess, ShellError>> = let result_vec: Vec<Result<ReturnSuccess, ShellError>> =
result.drain_vec().await; result.drain_vec().await;
if converter.is_binary() { if converter.is_binary() {

View File

@ -184,6 +184,10 @@ impl Context {
self.current_errors.lock().clone() self.current_errors.lock().clone()
} }
pub(crate) fn add_error(&self, err: ShellError) {
self.current_errors.lock().push(err);
}
pub(crate) fn maybe_print_errors(&mut self, source: Text) -> bool { pub(crate) fn maybe_print_errors(&mut self, source: Text) -> bool {
let errors = self.current_errors.clone(); let errors = self.current_errors.clone();
let mut errors = errors.lock(); let mut errors = errors.lock();
@ -232,7 +236,7 @@ impl Context {
args: hir::Call, args: hir::Call,
scope: &Scope, scope: &Scope,
input: InputStream, input: InputStream,
) -> OutputStream { ) -> Result<OutputStream, ShellError> {
let command_args = self.command_args(args, input, name_tag, scope); let command_args = self.command_args(args, input, name_tag, scope);
command.run(command_args, self.registry()).await command.run(command_args, self.registry()).await
} }

View File

@ -10,6 +10,16 @@ fn shows_error_for_command_not_found() {
assert!(actual.err.contains("Command not found")); assert!(actual.err.contains("Command not found"));
} }
#[test]
fn shows_error_for_command_not_found_in_pipeline() {
let actual = nu!(
cwd: ".",
"ferris_is_not_here.exe | echo done"
);
assert!(actual.err.contains("Command not found"));
}
#[test] #[test]
fn automatically_change_directory() { fn automatically_change_directory() {
use nu_test_support::playground::Playground; use nu_test_support::playground::Playground;