aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authorDaniel <kingdread@gmx.de>2020-04-21 13:59:28 +0200
committerDaniel <kingdread@gmx.de>2020-04-21 13:59:28 +0200
commit185a5b2f802f9d05c3eb40f807c0488f168c6661 (patch)
treeaef0c8939dc9ccb9b92538bc1306c6431bdefa64 /src/main.rs
parent0e4e148a0890ba206df40cffe5a5f1cc47c8079e (diff)
downloadraidgrep-185a5b2f802f9d05c3eb40f807c0488f168c6661.tar.gz
raidgrep-185a5b2f802f9d05c3eb40f807c0488f168c6661.tar.bz2
raidgrep-185a5b2f802f9d05c3eb40f807c0488f168c6661.zip
better error outputs
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs45
1 files changed, 42 insertions, 3 deletions
diff --git a/src/main.rs b/src/main.rs
index 8edd75a..2e0c82f 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,12 +1,14 @@
#![feature(trait_alias)]
use std::collections::HashMap;
+use std::fmt;
use std::fs::File;
use std::io::{BufReader, Read, Seek};
use std::path::PathBuf;
use std::str::FromStr;
-use anyhow::Result;
+use anyhow::{anyhow, Error, Result};
use chrono::{NaiveDateTime, Weekday};
+use colored::Colorize;
use log::debug;
use num_traits::cast::FromPrimitive;
use structopt::StructOpt;
@@ -157,10 +159,38 @@ impl<R: Read + Seek> ZipWrapper<R> {
}
}
+#[derive(Clone, Debug)]
+struct InputError {
+ line: String,
+ location: usize,
+ msg: String,
+}
+
+impl fmt::Display for InputError {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ let prefix = "Input:";
+ writeln!(f, "{} {}", prefix.yellow(), self.line)?;
+ let prefix_len = prefix.len() + self.location;
+ writeln!(f, "{}{}", " ".repeat(prefix_len), " ^-".red())?;
+ write!(f, "{}: {}", "Error".red(), self.msg)?;
+ Ok(())
+ }
+}
+
+impl std::error::Error for InputError {}
+
fn main() {
let result = run();
if let Err(err) = result {
- eprintln!("Error: {}", err);
+ display_error(&err);
+ }
+}
+
+fn display_error(err: &Error) {
+ if let Some(err) = err.downcast_ref::<InputError>() {
+ eprintln!("{}", err);
+ } else {
+ eprintln!("{}: {}", "Error".red(), err);
}
}
@@ -207,7 +237,16 @@ fn build_filter(opt: &Opt) -> Result<Box<dyn LogFilter>> {
// We're fine with the small memory leak, as we're only dealing with a small string in a
// short-lived program.
let expr_string = Box::leak(Box::new(opt.expression.join(" ")));
- Ok(fexpr::parse_logfilter(expr_string)?)
+ if expr_string.trim().is_empty() {
+ return Err(anyhow!("Expected a filter to be given"));
+ }
+ Ok(
+ fexpr::parse_logfilter(expr_string).map_err(|error| InputError {
+ line: expr_string.to_string(),
+ location: fexpr::location(&error),
+ msg: error.to_string(),
+ })?,
+ )
}
/// Run the grep search with the given options.