From 57240aa00d7a8f7cd611654c44bd04cec9192133 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 12 Nov 2021 16:02:32 +0100 Subject: 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. --- src/main.rs | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) (limited to 'src/main.rs') 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 { - Raw(Option), + Raw(R), Zipped(zip::ZipArchive), } impl ZipWrapper { 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 { + 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> { 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 { 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 { 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 Result> { 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 { -- cgit v1.2.3