From 50772428920c5f8c856e77a1b4ace9e99aef7196 Mon Sep 17 00:00:00 2001 From: Reilly Wood <26268125+rgwood@users.noreply.github.com> Date: Sun, 1 May 2022 00:33:41 -0700 Subject: [PATCH] Error printing changes for `watch` (#5389) * Move CliError to nu-protocol clean up comment * Enable printing errors instead of just returning them * Nicer Miette error printing in watch command --- crates/nu-cli/src/lib.rs | 2 -- crates/nu-cli/src/util.rs | 2 +- crates/nu-command/src/filesystem/watch.rs | 11 +++++------ crates/nu-command/src/viewers/table.rs | 15 +++++++++++---- .../errors.rs => nu-protocol/src/cli_error.rs} | 9 ++++++++- crates/nu-protocol/src/lib.rs | 2 ++ crates/nu-protocol/src/pipeline_data.rs | 10 ---------- 7 files changed, 27 insertions(+), 24 deletions(-) rename crates/{nu-cli/src/errors.rs => nu-protocol/src/cli_error.rs} (86%) diff --git a/crates/nu-cli/src/lib.rs b/crates/nu-cli/src/lib.rs index 3cce2c3b74..cd729486b3 100644 --- a/crates/nu-cli/src/lib.rs +++ b/crates/nu-cli/src/lib.rs @@ -1,7 +1,6 @@ mod commands; mod completions; mod config_files; -mod errors; mod eval_file; mod menus; mod nu_highlight; @@ -17,7 +16,6 @@ mod validation; pub use commands::evaluate_commands; pub use completions::{FileCompletion, NuCompleter}; pub use config_files::eval_config_contents; -pub use errors::CliError; pub use eval_file::evaluate_file; pub use menus::{DescriptionMenu, NuHelpCompleter}; pub use nu_highlight::NuHighlight; diff --git a/crates/nu-cli/src/util.rs b/crates/nu-cli/src/util.rs index 6d72f095f5..ab599a94bc 100644 --- a/crates/nu-cli/src/util.rs +++ b/crates/nu-cli/src/util.rs @@ -1,8 +1,8 @@ -use crate::CliError; use log::trace; use nu_engine::eval_block; use nu_parser::{escape_quote_string, lex, parse, unescape_unquote_string, Token, TokenContents}; use nu_protocol::engine::StateWorkingSet; +use nu_protocol::CliError; use nu_protocol::{ engine::{EngineState, Stack}, PipelineData, ShellError, Span, Value, diff --git a/crates/nu-command/src/filesystem/watch.rs b/crates/nu-command/src/filesystem/watch.rs index fecc3b25d0..8b0daead9c 100644 --- a/crates/nu-command/src/filesystem/watch.rs +++ b/crates/nu-command/src/filesystem/watch.rs @@ -6,10 +6,10 @@ use std::time::Duration; use notify::{DebouncedEvent, RecommendedWatcher, RecursiveMode, Watcher}; use nu_engine::{current_dir, eval_block, CallExt}; use nu_protocol::ast::Call; -use nu_protocol::engine::{CaptureBlock, Command, EngineState, Stack}; +use nu_protocol::engine::{CaptureBlock, Command, EngineState, Stack, StateWorkingSet}; use nu_protocol::{ - Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Spanned, SyntaxShape, - Value, + format_error, Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, + Spanned, SyntaxShape, Value, }; // durations chosen mostly arbitrarily @@ -219,9 +219,8 @@ impl Command for Watch { val.print(engine_state, stack)?; } Err(err) => { - // TODO: this isn't as nice as the Miette errors. Find a way to print those. - // Unfortunately can't just wrap err in PipelineData, PipelineData.print() doesn't work that way - eprintln!("{err:?}"); + let working_set = StateWorkingSet::new(engine_state); + eprintln!("{}", format_error(&working_set, &err)); } } } diff --git a/crates/nu-command/src/viewers/table.rs b/crates/nu-command/src/viewers/table.rs index fbbf7eb944..40141dd5d4 100644 --- a/crates/nu-command/src/viewers/table.rs +++ b/crates/nu-command/src/viewers/table.rs @@ -3,10 +3,10 @@ use nu_color_config::{get_color_config, style_primitive}; use nu_engine::column::get_columns; use nu_engine::{env_to_string, CallExt}; use nu_protocol::ast::{Call, PathMember}; -use nu_protocol::engine::{Command, EngineState, Stack}; +use nu_protocol::engine::{Command, EngineState, Stack, StateWorkingSet}; use nu_protocol::{ - Category, Config, DataSource, Example, IntoPipelineData, ListStream, PipelineData, - PipelineMetadata, RawStream, ShellError, Signature, Span, SyntaxShape, Value, + format_error, Category, Config, DataSource, Example, IntoPipelineData, ListStream, + PipelineData, PipelineMetadata, RawStream, ShellError, Signature, Span, SyntaxShape, Value, }; use nu_table::{StyledString, TextStyle, Theme}; use std::sync::atomic::{AtomicBool, Ordering}; @@ -141,7 +141,14 @@ impl Command for Table { } .into_pipeline_data()) } - PipelineData::Value(Value::Error { error }, ..) => Err(error), + PipelineData::Value(Value::Error { error }, ..) => { + let working_set = StateWorkingSet::new(engine_state); + Ok(Value::String { + val: format_error(&working_set, &error), + span: call.head, + } + .into_pipeline_data()) + } PipelineData::Value(Value::CustomValue { val, span }, ..) => { let base_pipeline = val.to_base_value(span)?.into_pipeline_data(); self.run(engine_state, stack, call, base_pipeline) diff --git a/crates/nu-cli/src/errors.rs b/crates/nu-protocol/src/cli_error.rs similarity index 86% rename from crates/nu-cli/src/errors.rs rename to crates/nu-protocol/src/cli_error.rs index 791c869fa4..2c794a2f92 100644 --- a/crates/nu-cli/src/errors.rs +++ b/crates/nu-protocol/src/cli_error.rs @@ -1,5 +1,5 @@ +use crate::engine::StateWorkingSet; use miette::{LabeledSpan, MietteHandler, ReportHandler, Severity, SourceCode}; -use nu_protocol::engine::StateWorkingSet; use thiserror::Error; /// This error exists so that we can defer SourceCode handling. It simply @@ -11,6 +11,13 @@ pub struct CliError<'src>( pub &'src StateWorkingSet<'src>, ); +pub fn format_error( + working_set: &StateWorkingSet, + error: &(dyn miette::Diagnostic + Send + Sync + 'static), +) -> String { + return format!("Error: {:?}", CliError(error, working_set)); +} + impl std::fmt::Debug for CliError<'_> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { MietteHandler::default().debug(self, f)?; diff --git a/crates/nu-protocol/src/lib.rs b/crates/nu-protocol/src/lib.rs index 9d8ab12668..7712a70c02 100644 --- a/crates/nu-protocol/src/lib.rs +++ b/crates/nu-protocol/src/lib.rs @@ -1,4 +1,5 @@ pub mod ast; +mod cli_error; mod config; pub mod engine; mod example; @@ -14,6 +15,7 @@ mod ty; mod value; mod variable; +pub use cli_error::*; pub use config::*; pub use engine::{ENV_VARIABLE_ID, IN_VARIABLE_ID, NU_VARIABLE_ID}; pub use example::*; diff --git a/crates/nu-protocol/src/pipeline_data.rs b/crates/nu-protocol/src/pipeline_data.rs index 39aa6323af..8cbcadfdca 100644 --- a/crates/nu-protocol/src/pipeline_data.rs +++ b/crates/nu-protocol/src/pipeline_data.rs @@ -455,11 +455,6 @@ impl PipelineData { for item in table { let stdout = std::io::stdout(); - - if let Value::Error { error } = item { - return Err(error); - } - let mut out = item.into_string("\n", config); out.push('\n'); @@ -472,11 +467,6 @@ impl PipelineData { None => { for item in self { let stdout = std::io::stdout(); - - if let Value::Error { error } = item { - return Err(error); - } - let mut out = item.into_string("\n", config); out.push('\n');