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::{
|
use std::{
|
||||||
|
boxed::Box,
|
||||||
|
convert::From,
|
||||||
error::Error,
|
error::Error,
|
||||||
fmt::{Display, Formatter, Result},
|
fmt::{Display, Formatter, Result},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use elefren::Error as elefrenError;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct ScootalooError {
|
pub struct ScootalooError {
|
||||||
details: String,
|
details: String,
|
||||||
@@ -23,3 +27,15 @@ impl Display for ScootalooError {
|
|||||||
write!(f, "{}", self.details)
|
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
|
// retrieve the last tweet ID for the username
|
||||||
let lconn = task_conn.lock().await;
|
let lconn = task_conn.lock().await;
|
||||||
let last_tweet_id = read_state(&lconn, &mastodon_config.twitter_screen_name, None)
|
let last_tweet_id =
|
||||||
.map_err(|e| ScootalooError::new(&format!("Cannot retrieve last_tweed_id: {}", e)))?
|
read_state(&lconn, &mastodon_config.twitter_screen_name, None)?.map(|r| r.tweet_id);
|
||||||
.map(|r| r.tweet_id);
|
|
||||||
drop(lconn);
|
drop(lconn);
|
||||||
|
|
||||||
// get user timeline feed (Vec<tweet>)
|
// get user timeline feed (Vec<tweet>)
|
||||||
let mut feed =
|
let mut feed =
|
||||||
get_user_timeline(&mastodon_config.twitter_screen_name, &token, last_tweet_id)
|
get_user_timeline(&mastodon_config.twitter_screen_name, &token, last_tweet_id)
|
||||||
.await
|
.await?;
|
||||||
.map_err(|e| {
|
|
||||||
ScootalooError::new(&format!(
|
|
||||||
"Something went wrong when trying to retrieve {}’s timeline: {}",
|
|
||||||
&mastodon_config.twitter_screen_name, e
|
|
||||||
))
|
|
||||||
})?;
|
|
||||||
|
|
||||||
// empty feed -> exiting
|
// empty feed -> exiting
|
||||||
if feed.is_empty() {
|
if feed.is_empty() {
|
||||||
@@ -126,20 +119,9 @@ pub async fn run(config: Config) {
|
|||||||
// can be activated for test purposes
|
// can be activated for test purposes
|
||||||
// status_builder.visibility(elefren::status_builder::Visibility::Private);
|
// status_builder.visibility(elefren::status_builder::Visibility::Private);
|
||||||
|
|
||||||
let status = status_builder.build().map_err(|e| {
|
let status = status_builder.build()?;
|
||||||
ScootalooError::new(&format!(
|
|
||||||
"Cannot build status with text \"{}\": {}",
|
|
||||||
&status_text, e
|
|
||||||
))
|
|
||||||
})?;
|
|
||||||
|
|
||||||
// publish status
|
let published_status = mastodon.new_status(status)?;
|
||||||
let published_status = mastodon.new_status(status).map_err(|e| {
|
|
||||||
ScootalooError::new(&format!(
|
|
||||||
"Cannot publish status from \"{}\": {}",
|
|
||||||
tweet.id, e
|
|
||||||
))
|
|
||||||
})?;
|
|
||||||
// this will return if it cannot publish the status preventing the last_tweet from
|
// this will return if it cannot publish the status preventing the last_tweet from
|
||||||
// being written into db
|
// 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
|
// write the current state (tweet ID and toot ID) to avoid copying it another time
|
||||||
let lconn = task_conn.lock().await;
|
let lconn = task_conn.lock().await;
|
||||||
if let Err(e) = write_state(&lconn, ttt_towrite) {
|
write_state(&lconn, ttt_towrite)?;
|
||||||
return Err(ScootalooError::new(&format!(
|
|
||||||
"Can’t write the last tweet retrieved: {}",
|
|
||||||
e
|
|
||||||
)));
|
|
||||||
};
|
|
||||||
drop(lconn);
|
drop(lconn);
|
||||||
}
|
}
|
||||||
Ok::<(), ScootalooError>(())
|
Ok::<(), ScootalooError>(())
|
||||||
|
Reference in New Issue
Block a user