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/output/aggregators.rs | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) (limited to 'src/output/aggregators.rs') diff --git a/src/output/aggregators.rs b/src/output/aggregators.rs index 34ecea4..c3d971a 100644 --- a/src/output/aggregators.rs +++ b/src/output/aggregators.rs @@ -14,10 +14,13 @@ use std::{ sync::Mutex, }; +use anyhow::Result; + pub trait Aggregator: Sync { - fn push_item(&self, item: LogResult, format: &dyn Format, stream: &mut dyn Write); + fn push_item(&self, item: LogResult, format: &dyn Format, stream: &mut dyn Write) + -> Result<()>; // When the `unsized_locals` feature is stable, we could rewrite this to finish(self, ...). - fn finish(self: Box, format: &dyn Format, stream: &mut dyn Write); + fn finish(self: Box, format: &dyn Format, stream: &mut dyn Write) -> Result<()>; } /// An aggregator that just pushes through each item to the output stream without any sorting or @@ -25,13 +28,21 @@ pub trait Aggregator: Sync { pub struct WriteThrough; impl Aggregator for WriteThrough { - fn push_item(&self, item: LogResult, format: &dyn Format, stream: &mut dyn Write) { + fn push_item( + &self, + item: LogResult, + format: &dyn Format, + stream: &mut dyn Write, + ) -> Result<()> { let text = format.format_result(&item); - stream.write_all(text.as_bytes()).unwrap(); - stream.flush().unwrap(); + stream.write_all(text.as_bytes())?; + stream.flush()?; + Ok(()) } - fn finish(self: Box, _: &dyn Format, _: &mut dyn Write) {} + fn finish(self: Box, _: &dyn Format, _: &mut dyn Write) -> Result<()> { + Ok(()) + } } /// An aggregator that keeps all found logs in memory and sorts them before outputting them. @@ -51,19 +62,21 @@ impl SortedOutput { } impl Aggregator for SortedOutput { - fn push_item(&self, item: LogResult, _: &dyn Format, _: &mut dyn Write) { - self.items.lock().unwrap().push(item) + fn push_item(&self, item: LogResult, _: &dyn Format, _: &mut dyn Write) -> Result<()> { + self.items.lock().unwrap().push(item); + Ok(()) } - fn finish(self: Box, format: &dyn Format, stream: &mut dyn Write) { + fn finish(self: Box, format: &dyn Format, stream: &mut dyn Write) -> Result<()> { let SortedOutput { sorting, items } = *self; let mut items = items.into_inner().unwrap(); items.sort_unstable_by(|a, b| sorting.cmp(a, b)); for item in items { let text = format.format_result(&item); - stream.write_all(text.as_bytes()).unwrap(); + stream.write_all(text.as_bytes())?; } - stream.flush().unwrap(); + stream.flush()?; + Ok(()) } } @@ -82,11 +95,13 @@ impl CountingOutput { } impl Aggregator for CountingOutput { - fn push_item(&self, _: LogResult, _: &dyn Format, _: &mut dyn Write) { + fn push_item(&self, _: LogResult, _: &dyn Format, _: &mut dyn Write) -> Result<()> { self.counter.fetch_add(1, Ordering::SeqCst); + Ok(()) } - fn finish(self: Box, _: &dyn Format, stream: &mut dyn Write) { - writeln!(stream, "{}", self.counter.into_inner()).unwrap(); + fn finish(self: Box, _: &dyn Format, stream: &mut dyn Write) -> Result<()> { + writeln!(stream, "{}", self.counter.into_inner())?; + Ok(()) } } -- cgit v1.2.3