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 | |
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')
-rw-r--r-- | src/output/aggregators.rs | 43 | ||||
-rw-r--r-- | src/output/formats.rs | 10 | ||||
-rw-r--r-- | src/output/pipeline.rs | 10 |
3 files changed, 40 insertions, 23 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(()) } } diff --git a/src/output/formats.rs b/src/output/formats.rs index 7225da2..d22ab19 100644 --- a/src/output/formats.rs +++ b/src/output/formats.rs @@ -29,7 +29,7 @@ impl Format for HumanReadable { "File".green(), item.log_file.to_string_lossy() ) - .unwrap(); + .expect("writing to String failed"); let outcome = match item.outcome { FightOutcome::Success => "SUCCESS".green(), FightOutcome::Wipe => "WIPE".red(), @@ -49,7 +49,7 @@ impl Format for HumanReadable { outcome, humantime::Duration::from(item.duration.to_std().unwrap()), ) - .unwrap(); + .expect("writing to String failed"); for player in &item.players { write!( result, @@ -59,7 +59,7 @@ impl Format for HumanReadable { player.character_name.cyan(), player.profession, ) - .unwrap(); + .expect("writing to String failed"); if self.show_guilds { let guild = player.guild_id.as_ref().and_then(|id| guilds::lookup(id)); if let Some(guild) = guild { @@ -69,10 +69,10 @@ impl Format for HumanReadable { guild.tag().magenta(), guild.name().magenta(), ) - .unwrap(); + .expect("writing to String failed"); } } - writeln!(result).unwrap(); + writeln!(result).expect("writing to String failed"); } result } diff --git a/src/output/pipeline.rs b/src/output/pipeline.rs index 9b7f461..3db0d56 100644 --- a/src/output/pipeline.rs +++ b/src/output/pipeline.rs @@ -3,6 +3,8 @@ use super::{aggregators::Aggregator, formats::Format}; use std::{io::Write, sync::Mutex}; +use anyhow::Result; + pub struct Pipeline { format: Box<dyn Format>, aggregator: Box<dyn Aggregator>, @@ -22,13 +24,13 @@ impl Pipeline { } } - pub fn push_item(&self, item: LogResult) { + pub fn push_item(&self, item: LogResult) -> Result<()> { let mut writer = self.writer.lock().unwrap(); - self.aggregator.push_item(item, &*self.format, &mut *writer); + self.aggregator.push_item(item, &*self.format, &mut *writer) } - pub fn finish(self) { + pub fn finish(self) -> Result<()> { let mut writer = self.writer.lock().unwrap(); - self.aggregator.finish(&*self.format, &mut *writer); + self.aggregator.finish(&*self.format, &mut *writer) } } |