From 4ede8e16e83ceb276303d8ee1b8a12ee492ef5a2 Mon Sep 17 00:00:00 2001 From: JMARyA Date: Sat, 14 Dec 2024 22:07:38 +0100 Subject: [PATCH] err handling --- src/library/video.rs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/library/video.rs b/src/library/video.rs index 8fe9d4b..e18afc9 100644 --- a/src/library/video.rs +++ b/src/library/video.rs @@ -8,6 +8,26 @@ use serde_json::json; use sqlx::prelude::FromRow; use std::path::PathBuf; +pub trait LogAndIgnore { + fn log_and_ignore(self, msg: &str); +} + +impl LogAndIgnore for Result { + /// Handles the result by ignoring and logging it if it contains an error. + /// + /// If the result is `Ok`, does nothing. + /// If the result is `Err(e)` + /// logs the message provided (`msg`) along with the error. + fn log_and_ignore(self, msg: &str) { + match self { + Ok(_) => {} + Err(e) => { + log::error!("{msg} : {:?}", e); + } + } + } +} + #[derive(Debug, Clone, Serialize, Deserialize, FromRow)] pub struct YouTubeMeta { pub id: String, @@ -132,7 +152,7 @@ impl Video { .bind(cat) .execute(&mut *tx) .await - .unwrap(); + .log_and_ignore("Category mapping already exists"); } if let Some(tags) = meta.tags() { @@ -142,7 +162,7 @@ impl Video { .bind(&tag) .execute(&mut *tx) .await - .unwrap(); + .log_and_ignore("YT Tag mapping already exists"); } }