From 07abc4e49a1cd4e7175262998f9c8e9dc09097ec Mon Sep 17 00:00:00 2001 From: taevel02 Date: Sun, 11 Feb 2024 22:21:30 +0900 Subject: [PATCH] fix: handle errors --- src-tauri/src/error.rs | 6 ++++++ src-tauri/src/producer.rs | 26 ++++++++++---------------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src-tauri/src/error.rs b/src-tauri/src/error.rs index d65c628..cc3c7be 100644 --- a/src-tauri/src/error.rs +++ b/src-tauri/src/error.rs @@ -17,6 +17,12 @@ pub enum Error { #[error("failed to parse syndication feed")] SyndicationParsingFailure, + #[error("failed to fetch feed: {0}")] + FetchFeedFailure(String), + + #[error("failed to fetch feed items: {0}")] + FetchFeedItemsFailure(String), + #[error("empty string")] EmptyString, diff --git a/src-tauri/src/producer.rs b/src-tauri/src/producer.rs index fc73644..bfb6f49 100644 --- a/src-tauri/src/producer.rs +++ b/src-tauri/src/producer.rs @@ -13,7 +13,9 @@ use crate::{ syndication::fetch_feed_items, }; -pub fn create_new_items(db: &Connection, proxy: Option<&str>) -> Result, String> { +use crate::error::{Error, Result}; + +pub fn create_new_items(db: &Connection, proxy: Option<&str>) -> Result> { let pairs = get_links_to_check(db); let mut inserted = vec![]; @@ -25,7 +27,7 @@ pub fn create_new_items(db: &Connection, proxy: Option<&str>) -> Result Some(items), - Err(err) => return Err(format!("Error fetching most recent items: {}", err)), + Err(err) => return Err(Error::FetchFeedItemsFailure(err.to_string())), }; } } @@ -46,7 +48,7 @@ pub fn create_new_items(db: &Connection, proxy: Option<&str>) -> Result return Err(format!("Error fetching feed items: {}", err)), + Err(err) => return Err(Error::FetchFeedItemsFailure(err.to_string())), } } @@ -107,13 +109,10 @@ fn insert_new_items(db: &Connection, feed: i32, items: &[RawItem]) -> Vec Result>, String> { +fn get_most_recent_items(db: &Connection) -> Result>> { let mut most_recent_items = HashMap::new(); - let feed_ids = match get_all_feed_ids(db) { - Ok(ids) => ids, - Err(err) => return Err(format!("Failed to fetch feed ids: {}", err)), - }; + let feed_ids = get_all_feed_ids(db)?; for feed_id in feed_ids { let opt = ItemReadOption { @@ -132,21 +131,16 @@ fn get_most_recent_items(db: &Connection) -> Result { - return Err(format!( - "Failed to fetch items for feed {}: {}", - feed_id, err - )) - } + Err(err) => return Err(Error::InvalidValue(err.to_string())), } } Ok(most_recent_items) } -fn get_all_feed_ids(db: &Connection) -> Result, String> { +fn get_all_feed_ids(db: &Connection) -> Result> { match feeds::read_all(db) { Ok(feeds) => Ok(feeds.iter().map(|x| x.id).collect()), - Err(err) => Err(err.to_string()), + Err(err) => Err(Error::FetchFeedFailure(err.to_string())), } }