mirror of
https://framagit.org/veretcle/scootaloo.git
synced 2025-07-20 17:11:19 +02:00
refactor: avoid panicking into thread, bubble up errors to main thread to be handled
This commit is contained in:
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -2016,7 +2016,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "scootaloo"
|
name = "scootaloo"
|
||||||
version = "0.8.0"
|
version = "0.8.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"chrono",
|
"chrono",
|
||||||
"clap",
|
"clap",
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "scootaloo"
|
name = "scootaloo"
|
||||||
version = "0.8.0"
|
version = "0.8.1"
|
||||||
authors = ["VC <veretcle+framagit@mateu.be>"]
|
authors = ["VC <veretcle+framagit@mateu.be>"]
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
|
50
src/lib.rs
50
src/lib.rs
@@ -53,25 +53,25 @@ 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 = read_state(&lconn, &mastodon_config.twitter_screen_name, None)
|
||||||
.unwrap_or_else(|e| panic!("Cannot retrieve last_tweet_id: {}", e))
|
.map_err(|e| ScootalooError::new(&format!("Cannot retrieve last_tweed_id: {}", e)))?
|
||||||
.map(|s| s.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
|
||||||
.unwrap_or_else(|e| {
|
.map_err(|e| {
|
||||||
panic!(
|
ScootalooError::new(&format!(
|
||||||
"Something went wrong when trying to retrieve {}’s timeline: {}",
|
"Something went wrong when trying to retrieve {}’s timeline: {}",
|
||||||
&mastodon_config.twitter_screen_name, e
|
&mastodon_config.twitter_screen_name, e
|
||||||
)
|
))
|
||||||
});
|
})?;
|
||||||
|
|
||||||
// empty feed -> exiting
|
// empty feed -> exiting
|
||||||
if feed.is_empty() {
|
if feed.is_empty() {
|
||||||
info!("Nothing to retrieve since last time, exiting…");
|
info!("Nothing to retrieve since last time, exiting…");
|
||||||
return;
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
// get Mastodon instance
|
// get Mastodon instance
|
||||||
@@ -126,15 +126,22 @@ 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
|
let status = status_builder.build().map_err(|e| {
|
||||||
.build()
|
ScootalooError::new(&format!(
|
||||||
.unwrap_or_else(|_| panic!("Cannot build status with text {}", &status_text));
|
"Cannot build status with text \"{}\": {}",
|
||||||
|
&status_text, e
|
||||||
|
))
|
||||||
|
})?;
|
||||||
|
|
||||||
// publish status
|
// publish status
|
||||||
// again unwrap is safe here as we are in the main thread
|
let published_status = mastodon.new_status(status).map_err(|e| {
|
||||||
let published_status = mastodon.new_status(status).unwrap();
|
ScootalooError::new(&format!(
|
||||||
// this will panic if it cannot publish the status, which is a good thing, it allows the
|
"Cannot publish status from \"{}\": {}",
|
||||||
// last_tweet gathered not to be written
|
tweet.id, e
|
||||||
|
))
|
||||||
|
})?;
|
||||||
|
// this will return if it cannot publish the status preventing the last_tweet from
|
||||||
|
// being written into db
|
||||||
|
|
||||||
let ttt_towrite = TweetToToot {
|
let ttt_towrite = TweetToToot {
|
||||||
twitter_screen_name: mastodon_config.twitter_screen_name.clone(),
|
twitter_screen_name: mastodon_config.twitter_screen_name.clone(),
|
||||||
@@ -144,10 +151,15 @@ 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;
|
||||||
write_state(&lconn, ttt_towrite)
|
if let Err(e) = write_state(&lconn, ttt_towrite) {
|
||||||
.unwrap_or_else(|e| panic!("Can’t write the last tweet retrieved: {}", e));
|
return Err(ScootalooError::new(&format!(
|
||||||
|
"Can’t write the last tweet retrieved: {}",
|
||||||
|
e
|
||||||
|
)));
|
||||||
|
};
|
||||||
drop(lconn);
|
drop(lconn);
|
||||||
}
|
}
|
||||||
|
Ok::<(), ScootalooError>(())
|
||||||
});
|
});
|
||||||
|
|
||||||
// push each task into the vec task
|
// push each task into the vec task
|
||||||
@@ -156,6 +168,10 @@ pub async fn run(config: Config) {
|
|||||||
|
|
||||||
// launch and wait for every handle
|
// launch and wait for every handle
|
||||||
for handle in mtask {
|
for handle in mtask {
|
||||||
handle.await.unwrap();
|
match handle.await {
|
||||||
|
Ok(Err(e)) => eprintln!("Error within thread: {}", e),
|
||||||
|
Err(e) => eprintln!("Error with thread: {}", e),
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user