diff --git a/cli/src/error.rs b/cli/src/error.rs index 5c9c2bf7..5d3f843d 100644 --- a/cli/src/error.rs +++ b/cli/src/error.rs @@ -5,26 +5,35 @@ use tree_sitter::{QueryError, QueryErrorKind}; use walkdir; #[derive(Debug)] -pub struct Error(pub Vec); +pub struct Error(Option>); pub type Result = std::result::Result; impl Error { pub fn grammar(message: &str) -> Self { - Error(vec![format!("Grammar error: {}", message)]) + Error(Some(vec![format!("Grammar error: {}", message)])) } pub fn regex(mut message: String) -> Self { message.insert_str(0, "Regex error: "); - Error(vec![message]) + Error(Some(vec![message])) } pub fn undefined_symbol(name: &str) -> Self { - Error(vec![format!("Undefined symbol `{}`", name)]) + Error(Some(vec![format!("Undefined symbol `{}`", name)])) } pub fn new(message: String) -> Self { - Error(vec![message]) + let e = Error(Some(vec![message])); + e + } + + pub fn new_ignored() -> Self { + Self(None) + } + + pub fn is_ignored(&self) -> bool { + self.0.is_none() } pub fn err(message: String) -> Result { @@ -36,20 +45,28 @@ impl Error { ) -> impl FnOnce(E) -> Self { |e| { let mut result = e.into(); - result.0.push(message_fn().to_string()); + match result.0 { + Some(ref mut e) => e.push(message_fn().to_string()), + None => panic!("It's not allowed to wrap an ignored error"), + } result } } pub fn message(&self) -> String { - let mut result = self.0.last().unwrap().clone(); - if self.0.len() > 1 { - result.push_str("\nDetails:\n"); - for msg in self.0[0..self.0.len() - 1].iter().rev() { - writeln!(&mut result, " {}", msg).unwrap(); + match self.0 { + None => "Ignored error".to_string(), + Some(ref e) => { + let mut result = e.last().unwrap().clone(); + if e.len() > 1 { + result.push_str("\nDetails:\n"); + for msg in e[0..e.len() - 1].iter().rev() { + writeln!(&mut result, " {}", msg).unwrap(); + } + } + result } } - result } } @@ -89,6 +106,10 @@ impl From for Error { impl From for Error { fn from(error: io::Error) -> Self { + match error.raw_os_error() { + Some(32) => return Error::new_ignored(), // Broken pipe + _ => (), + } Error::new(error.to_string()) } } diff --git a/cli/src/main.rs b/cli/src/main.rs index a2d0a7da..7c2774fe 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -14,9 +14,12 @@ const BUILD_VERSION: &'static str = env!("CARGO_PKG_VERSION"); const BUILD_SHA: Option<&'static str> = option_env!("BUILD_SHA"); fn main() { - if let Err(e) = run() { + if let Err(ref e) = run() { + if e.is_ignored() { + exit(0); + } if !e.message().is_empty() { - println!(""); + eprintln!(""); eprintln!("{}", e.message()); } exit(1);