mirror of
https://framagit.org/veretcle/scootaloo.git
synced 2025-07-20 17:11:19 +02:00
feature: better error implementation for ScootalooError inside async block
This commit is contained in:
16
src/error.rs
16
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<Box<dyn Error>> for ScootalooError {
|
||||
fn from(error: Box<dyn Error>) -> Self {
|
||||
ScootalooError::new(&format!("Error in a subset crate: {}", error))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<elefrenError> for ScootalooError {
|
||||
fn from(error: elefrenError) -> Self {
|
||||
ScootalooError::new(&format!("Error in elefren crate: {}", error))
|
||||
}
|
||||
}
|
||||
|
35
src/lib.rs
35
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<tweet>)
|
||||
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>(())
|
||||
|
Reference in New Issue
Block a user