From f77acc1afd8773864ab81de1322ee734f3e6a3c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Thu, 12 Jun 2025 23:28:42 +0000 Subject: [PATCH] cleanup some `unwrap()` --- src/appstate.rs | 13 ++++++++----- src/cachefile.rs | 2 +- src/main.rs | 26 ++++++++++++++++++++++---- 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/src/appstate.rs b/src/appstate.rs index b7e4709..ca68a44 100644 --- a/src/appstate.rs +++ b/src/appstate.rs @@ -6,7 +6,7 @@ use std::{ use console::style; use indicatif::{ProgressBar, ProgressStyle}; -use log::debug; +use log::{debug, warn}; use super::{ cachefile::CacheFile, @@ -89,7 +89,7 @@ impl AppState { ), style("/").magenta(), )) - .unwrap(), // safe as long as the style template is valid + .expect("style template is not valid"), ) .with_position(uploading.get_offset()) .with_message(uploading.get_name().to_owned()); @@ -99,8 +99,9 @@ impl AppState { } drop(slot); - // unwrap is safe: We just made sure it's `Some`. - Ref::map(self.current_bar.borrow(), |opt| opt.as_ref().unwrap()) + Ref::map(self.current_bar.borrow(), |opt| { + opt.as_ref().expect("somehow the slot holds None") + }) } fn finish_bar(&self) { @@ -113,7 +114,9 @@ impl AppState { pub fn upload_chunk(&mut self) -> sharry::Result> { let Some(mut uploading) = self.inner.pop_file(&self.http) else { - self.inner.share_notify(&self.http).unwrap(); // HACK unwrap + self.inner + .share_notify(&self.http) + .unwrap_or_else(|e| warn!("Failed to notify the share: {e}")); return Ok(None); }; diff --git a/src/cachefile.rs b/src/cachefile.rs index fe9b3a9..6f11312 100644 --- a/src/cachefile.rs +++ b/src/cachefile.rs @@ -108,7 +108,7 @@ impl CacheFile { let endpoint = self .uri .endpoint(format!("alias/upload/{}/files/tus", self.share_id)); - Some(state.start_upload(http, &endpoint, &self.alias_id).unwrap()) // HACK unwrap + Some(state.start_upload(http, &endpoint, &self.alias_id).unwrap()) // HACK unwrap, somehow retry } else { None } diff --git a/src/main.rs b/src/main.rs index 2ca9d32..d9b5bb8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -89,7 +89,13 @@ fn main() { match AppState::from_args(&args) { Ok(state) => { - state.save().unwrap(); // HACK unwrap + state.save().unwrap_or_else(|e| { + eprintln!( + "{} Failed to save {} state: {e}", + style("Warning:").red().bold(), + style("ShrUpl").yellow().bold(), + ) + }); state } Err(e) => { @@ -109,16 +115,28 @@ fn main() { loop { match state.upload_chunk() { - Err(e) => error!("error: {e:?}"), + Err(e) => error!("error: {e:?}"), // HACK handle errors better Ok(None) => { info!("all uploads done"); - state.clear().unwrap(); // HACK unwrap + state.clear().unwrap_or_else(|e| { + eprintln!( + "{} Failed to remove {} state: {e}", + style("Warning:").red().bold(), + style("ShrUpl").yellow().bold(), + ) + }); break; } _ => (), } - state.save().unwrap(); // HACK unwrap + state.save().unwrap_or_else(|e| { + eprintln!( + "{} Failed to save {} state: {e}", + style("Warning:").red().bold(), + style("ShrUpl").yellow().bold(), + ) + }); check_ctrlc(); } }