aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authorDaniel <kingdread@gmx.de>2021-11-12 16:02:32 +0100
committerDaniel <kingdread@gmx.de>2021-11-12 16:02:32 +0100
commit57240aa00d7a8f7cd611654c44bd04cec9192133 (patch)
tree0e748f1f6b64f3787047dffc69c0f4d6a7d83aff /src/main.rs
parent2e2bdac00092f8fcb96283da697a7a820a8c8978 (diff)
downloadraidgrep-57240aa00d7a8f7cd611654c44bd04cec9192133.tar.gz
raidgrep-57240aa00d7a8f7cd611654c44bd04cec9192133.tar.bz2
raidgrep-57240aa00d7a8f7cd611654c44bd04cec9192133.zip
Better error handling, less .unwraps()
Some of these unwraps are fine to stay, mostly those that deal with locks - in this case, crashing the program if something goes wrong is probably fine. However, we also had a lot of other places where we panic'd on errors, even though we really shouldn't have. For example, an invalid zip file would bring down the whole scanner. In this case, we now use proper Result<>s and we log the error. Some places stay with unwrap() for now, mainly the code that is rare and obvious when it goes wrong - such as an overflow in input values. It could be made nicer, but it is not a priority for now. Some unwraps() have been changed to expect() to signal why they shouldn't fail.
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs28
1 files changed, 15 insertions, 13 deletions
diff --git a/src/main.rs b/src/main.rs
index 27ad285..242e285 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,4 +1,4 @@
-#![feature(trait_alias)]
+#![feature(trait_alias, try_blocks)]
use std::collections::HashMap;
use std::fmt;
use std::fs::{self, File};
@@ -10,7 +10,7 @@ use std::sync::atomic::{AtomicBool, Ordering};
use anyhow::{anyhow, Context, Error, Result};
use chrono::{DateTime, Duration, TimeZone, Utc};
use colored::Colorize;
-use log::debug;
+use log::{debug, error};
use regex::Regex;
use rustyline::Editor;
use structopt::StructOpt;
@@ -253,23 +253,23 @@ pub struct EarlyLogResult {
}
enum ZipWrapper<R: Read + Seek> {
- Raw(Option<R>),
+ Raw(R),
Zipped(zip::ZipArchive<R>),
}
impl<R: Read + Seek> ZipWrapper<R> {
pub fn raw(input: R) -> Self {
- ZipWrapper::Raw(Some(input))
+ ZipWrapper::Raw(input)
}
- pub fn zipped(input: R) -> Self {
- ZipWrapper::Zipped(zip::ZipArchive::new(input).unwrap())
+ pub fn zipped(input: R) -> Result<Self> {
+ Ok(ZipWrapper::Zipped(zip::ZipArchive::new(input)?))
}
- pub fn get_stream<'a>(&'a mut self) -> Box<(dyn Read + 'a)> {
+ pub fn get_stream<'a>(&'a mut self) -> Result<Box<(dyn Read + 'a)>> {
match *self {
- ZipWrapper::Raw(ref mut o) => Box::new(o.take().unwrap()),
- ZipWrapper::Zipped(ref mut z) => Box::new(z.by_index(0).unwrap()),
+ ZipWrapper::Raw(ref mut o) => Ok(Box::new(o)),
+ ZipWrapper::Zipped(ref mut z) => Ok(Box::new(z.by_index(0)?)),
}
}
}
@@ -471,7 +471,9 @@ fn grep(opt: &Opt, filter: &dyn LogFilter) -> Result<bool> {
Ok(None) => (),
Ok(Some(result)) => {
found_something.store(true, Ordering::Relaxed);
- pipeline_ref.push_item(result);
+ if let Err(e) = pipeline_ref.push_item(result) {
+ error!("Error writing item to output: {}", e);
+ }
}
Err(err) => {
debug!("Runtime error while scanning {:?}: {}", entry.path(), err);
@@ -483,7 +485,7 @@ fn grep(opt: &Opt, filter: &dyn LogFilter) -> Result<bool> {
Ok(())
});
result?;
- pipeline.finish();
+ pipeline.finish()?;
Ok(found_something.load(Ordering::Relaxed))
}
@@ -512,11 +514,11 @@ fn search_entry(entry: &DirEntry, filter: &dyn LogFilter) -> Result<Option<LogRe
fn search_file(path: &Path, is_zip: bool, filter: &dyn LogFilter) -> Result<Option<LogResult>> {
let file_stream = BufReader::new(File::open(path)?);
let mut wrapper = if is_zip {
- ZipWrapper::zipped(file_stream)
+ ZipWrapper::zipped(file_stream)?
} else {
ZipWrapper::raw(file_stream)
};
- let mut stream = wrapper.get_stream();
+ let mut stream = wrapper.get_stream()?;
let partial = evtclib::raw::parser::parse_partial_file(&mut stream)?;
let early_log = EarlyLogResult {