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]]
|
||||
name = "scootaloo"
|
||||
version = "0.8.0"
|
||||
version = "0.8.1"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"clap",
|
||||
|
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "scootaloo"
|
||||
version = "0.8.0"
|
||||
version = "0.8.1"
|
||||
authors = ["VC <veretcle+framagit@mateu.be>"]
|
||||
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
|
||||
let lconn = task_conn.lock().await;
|
||||
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(|s| s.tweet_id);
|
||||
.map_err(|e| ScootalooError::new(&format!("Cannot retrieve last_tweed_id: {}", e)))?
|
||||
.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
|
||||
.unwrap_or_else(|e| {
|
||||
panic!(
|
||||
.map_err(|e| {
|
||||
ScootalooError::new(&format!(
|
||||
"Something went wrong when trying to retrieve {}’s timeline: {}",
|
||||
&mastodon_config.twitter_screen_name, e
|
||||
)
|
||||
});
|
||||
))
|
||||
})?;
|
||||
|
||||
// empty feed -> exiting
|
||||
if feed.is_empty() {
|
||||
info!("Nothing to retrieve since last time, exiting…");
|
||||
return;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// get Mastodon instance
|
||||
@@ -126,15 +126,22 @@ 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()
|
||||
.unwrap_or_else(|_| panic!("Cannot build status with text {}", &status_text));
|
||||
let status = status_builder.build().map_err(|e| {
|
||||
ScootalooError::new(&format!(
|
||||
"Cannot build status with text \"{}\": {}",
|
||||
&status_text, e
|
||||
))
|
||||
})?;
|
||||
|
||||
// publish status
|
||||
// again unwrap is safe here as we are in the main thread
|
||||
let published_status = mastodon.new_status(status).unwrap();
|
||||
// this will panic if it cannot publish the status, which is a good thing, it allows the
|
||||
// last_tweet gathered not to be written
|
||||
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
|
||||
// being written into db
|
||||
|
||||
let ttt_towrite = TweetToToot {
|
||||
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
|
||||
let lconn = task_conn.lock().await;
|
||||
write_state(&lconn, ttt_towrite)
|
||||
.unwrap_or_else(|e| panic!("Can’t write the last tweet retrieved: {}", e));
|
||||
if let Err(e) = write_state(&lconn, ttt_towrite) {
|
||||
return Err(ScootalooError::new(&format!(
|
||||
"Can’t write the last tweet retrieved: {}",
|
||||
e
|
||||
)));
|
||||
};
|
||||
drop(lconn);
|
||||
}
|
||||
Ok::<(), ScootalooError>(())
|
||||
});
|
||||
|
||||
// push each task into the vec task
|
||||
@@ -156,6 +168,10 @@ pub async fn run(config: Config) {
|
||||
|
||||
// launch and wait for every handle
|
||||
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