mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 01:15:14 +02:00
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
This commit is contained in:
61
crates/nu-protocol/src/cli_error.rs
Normal file
61
crates/nu-protocol/src/cli_error.rs
Normal file
@ -0,0 +1,61 @@
|
||||
use crate::engine::StateWorkingSet;
|
||||
use miette::{LabeledSpan, MietteHandler, ReportHandler, Severity, SourceCode};
|
||||
use thiserror::Error;
|
||||
|
||||
/// This error exists so that we can defer SourceCode handling. It simply
|
||||
/// forwards most methods, except for `.source_code()`, which we provide.
|
||||
#[derive(Error)]
|
||||
#[error("{0}")]
|
||||
pub struct CliError<'src>(
|
||||
pub &'src (dyn miette::Diagnostic + Send + Sync + 'static),
|
||||
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)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'src> miette::Diagnostic for CliError<'src> {
|
||||
fn code<'a>(&'a self) -> Option<Box<dyn std::fmt::Display + 'a>> {
|
||||
self.0.code()
|
||||
}
|
||||
|
||||
fn severity(&self) -> Option<Severity> {
|
||||
self.0.severity()
|
||||
}
|
||||
|
||||
fn help<'a>(&'a self) -> Option<Box<dyn std::fmt::Display + 'a>> {
|
||||
self.0.help()
|
||||
}
|
||||
|
||||
fn url<'a>(&'a self) -> Option<Box<dyn std::fmt::Display + 'a>> {
|
||||
self.0.url()
|
||||
}
|
||||
|
||||
fn labels<'a>(&'a self) -> Option<Box<dyn Iterator<Item = LabeledSpan> + 'a>> {
|
||||
self.0.labels()
|
||||
}
|
||||
|
||||
// Finally, we redirect the source_code method to our own source.
|
||||
fn source_code(&self) -> Option<&dyn SourceCode> {
|
||||
if let Some(source_code) = self.0.source_code() {
|
||||
Some(source_code)
|
||||
} else {
|
||||
Some(&self.1)
|
||||
}
|
||||
}
|
||||
|
||||
fn related<'a>(&'a self) -> Option<Box<dyn Iterator<Item = &'a dyn miette::Diagnostic> + 'a>> {
|
||||
self.0.related()
|
||||
}
|
||||
}
|
@ -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::*;
|
||||
|
@ -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');
|
||||
|
||||
|
Reference in New Issue
Block a user