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/formats.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/output/formats.rs') 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 } -- cgit v1.2.3