From 4f5663b450d9d4cba0dd73481a0479ea01ad2e4d Mon Sep 17 00:00:00 2001 From: VC Date: Wed, 9 Nov 2022 19:36:00 +0100 Subject: [PATCH] feature: better error implementation for ScootalooError inside async block --- src/error.rs | 16 ++++++++++++++++ src/lib.rs | 35 ++++++----------------------------- 2 files changed, 22 insertions(+), 29 deletions(-) diff --git a/src/error.rs b/src/error.rs index 9055477..b95cdc9 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,8 +1,12 @@ use std::{ + boxed::Box, + convert::From, error::Error, fmt::{Display, Formatter, Result}, }; +use elefren::Error as elefrenError; + #[derive(Debug)] pub struct ScootalooError { details: String, @@ -23,3 +27,15 @@ impl Display for ScootalooError { write!(f, "{}", self.details) } } + +impl From> for ScootalooError { + fn from(error: Box) -> Self { + ScootalooError::new(&format!("Error in a subset crate: {}", error)) + } +} + +impl From for ScootalooError { + fn from(error: elefrenError) -> Self { + ScootalooError::new(&format!("Error in elefren crate: {}", error)) + } +} diff --git a/src/lib.rs b/src/lib.rs index e886754..f5e0822 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -52,21 +52,14 @@ pub async fn run(config: Config) { // retrieve the last tweet ID for the username let lconn = task_conn.lock().await; - let last_tweet_id = read_state(&lconn, &mastodon_config.twitter_screen_name, None) - .map_err(|e| ScootalooError::new(&format!("Cannot retrieve last_tweed_id: {}", e)))? - .map(|r| r.tweet_id); + let last_tweet_id = + read_state(&lconn, &mastodon_config.twitter_screen_name, None)?.map(|r| r.tweet_id); drop(lconn); // get user timeline feed (Vec) let mut feed = get_user_timeline(&mastodon_config.twitter_screen_name, &token, last_tweet_id) - .await - .map_err(|e| { - ScootalooError::new(&format!( - "Something went wrong when trying to retrieve {}’s timeline: {}", - &mastodon_config.twitter_screen_name, e - )) - })?; + .await?; // empty feed -> exiting if feed.is_empty() { @@ -126,20 +119,9 @@ pub async fn run(config: Config) { // can be activated for test purposes // status_builder.visibility(elefren::status_builder::Visibility::Private); - let status = status_builder.build().map_err(|e| { - ScootalooError::new(&format!( - "Cannot build status with text \"{}\": {}", - &status_text, e - )) - })?; + let status = status_builder.build()?; - // publish status - let published_status = mastodon.new_status(status).map_err(|e| { - ScootalooError::new(&format!( - "Cannot publish status from \"{}\": {}", - tweet.id, e - )) - })?; + let published_status = mastodon.new_status(status)?; // this will return if it cannot publish the status preventing the last_tweet from // being written into db @@ -151,12 +133,7 @@ pub async fn run(config: Config) { // write the current state (tweet ID and toot ID) to avoid copying it another time let lconn = task_conn.lock().await; - if let Err(e) = write_state(&lconn, ttt_towrite) { - return Err(ScootalooError::new(&format!( - "Can’t write the last tweet retrieved: {}", - e - ))); - }; + write_state(&lconn, ttt_towrite)?; drop(lconn); } Ok::<(), ScootalooError>(())