diff --git a/src/client/youtube.rs b/src/client/youtube.rs index 6687095..8378015 100644 --- a/src/client/youtube.rs +++ b/src/client/youtube.rs @@ -68,7 +68,14 @@ impl YoutubeClient { let insert_call = self.client.videos().insert(video); trace!("Starting resumable upload"); let upload = insert_call - .upload_resumable(stream.into_std().await, "video/mp4".parse().unwrap()) + .upload_resumable( + stream.into_std().await, + "video/mp4".parse().map_err(|_| { + UploaderError::Unreachable( + "Could not parse 'video/mp4' mime type. This mime type needs to always be valid.".to_string(), + ) + })?, + ) .await; trace!("Resumable upload finished"); let result_str = if upload.is_ok() { "Ok" } else { "Error" }; @@ -127,7 +134,10 @@ impl YoutubeClient { ..Default::default() }; let playlist_insert_call = self.client.playlists().insert(playlist); - let (_, playlist) = playlist_insert_call.doit().await.unwrap(); + let (_, playlist) = playlist_insert_call + .doit() + .await + .map_err(UploaderError::YoutubeError)?; playlist.id.ok_or(UploaderError::NoIdReturned) } @@ -142,7 +152,7 @@ impl Debug for YoutubeClient { impl YoutubeClient { #[tracing::instrument(skip(user), fields(user.id = user.as_ref().map(|x| x.id),user.twitch_id = user.as_ref().map(|x| &x.twitch_id)))] pub async fn new(scopes: &Vec, user: Option) -> Result { - let hyper_client = Self::create_hyper_client(); + let hyper_client = Self::create_hyper_client()?; let application_secret_path = PathBuf::from( &shellexpand::full(&crate::CONF.google.youtube.client_secret_path) .map_err(UploaderError::ExpandPath)? @@ -159,15 +169,15 @@ impl YoutubeClient { Ok(Self { client }) } - fn create_hyper_client() -> Client> { - hyper::Client::builder().build( + fn create_hyper_client() -> Result>> { + Ok(hyper::Client::builder().build( HttpsConnectorBuilder::new() .with_native_roots() - .expect("could not get native roots") + .map_err(UploaderError::CreateClient)? .https_or_http() .enable_http1() .enable_http2() .build(), - ) + )) } } diff --git a/src/client/youtube/data.rs b/src/client/youtube/data.rs index de85307..992af30 100644 --- a/src/client/youtube/data.rs +++ b/src/client/youtube/data.rs @@ -160,10 +160,10 @@ fn substitute_common( fn shorten_string_if_needed(s: impl Into, target_len: Option) -> String { let s = s.into(); const SHORTEN_CHARS: &str = "..."; - if target_len.is_none() { - return s; - } - let target_len = target_len.unwrap(); + let target_len = match target_len { + Some(target_len) => target_len, + None => return s, + }; if target_len < SHORTEN_CHARS.len() { return SHORTEN_CHARS[..target_len].to_string(); } diff --git a/src/errors.rs b/src/errors.rs index 6915f2b..06628ff 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -9,6 +9,9 @@ pub enum UploaderError { #[error("Path could not be expanded")] ExpandPath(#[source] LookupError), + #[error("Got an auth error: {0}")] + CreateClient(#[source] std::io::Error), + #[error("Got an auth error: {0}")] AuthError(#[from] AuthError), @@ -44,6 +47,9 @@ pub enum UploaderError { PartCountMismatch(usize, usize), #[error("no id returned from youtube")] NoIdReturned, + + #[error("This error should be unreachable: {0}")] + Unreachable(String), } #[derive(Debug, thiserror::Error)]