diff options
author | Daniel <kingdread@gmx.de> | 2021-11-12 16:02:32 +0100 |
---|---|---|
committer | Daniel <kingdread@gmx.de> | 2021-11-12 16:02:32 +0100 |
commit | 57240aa00d7a8f7cd611654c44bd04cec9192133 (patch) | |
tree | 0e748f1f6b64f3787047dffc69c0f4d6a7d83aff /src/output/aggregators.rs | |
parent | 2e2bdac00092f8fcb96283da697a7a820a8c8978 (diff) | |
download | raidgrep-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/output/aggregators.rs')
-rw-r--r-- | src/output/aggregators.rs | 43 |
1 files changed, 29 insertions, 14 deletions
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<Self>, format: &dyn Format, stream: &mut dyn Write); + fn finish(self: Box<Self>, 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<Self>, _: &dyn Format, _: &mut dyn Write) {} + fn finish(self: Box<Self>, _: &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<Self>, format: &dyn Format, stream: &mut dyn Write) { + fn finish(self: Box<Self>, 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<Self>, _: &dyn Format, stream: &mut dyn Write) { - writeln!(stream, "{}", self.counter.into_inner()).unwrap(); + fn finish(self: Box<Self>, _: &dyn Format, stream: &mut dyn Write) -> Result<()> { + writeln!(stream, "{}", self.counter.into_inner())?; + Ok(()) } } |