mirror of
https://framagit.org/veretcle/scootaloo.git
synced 2025-07-20 17:11:19 +02:00
refactor: simpler error bubbling inside async block
This commit is contained in:
27
src/util.rs
27
src/util.rs
@@ -45,33 +45,16 @@ pub async fn generate_media_ids(
|
||||
let task = tokio::task::spawn(async move {
|
||||
info!("Start treating {}", media.media_url_https);
|
||||
// get the tweet embedded media
|
||||
let local_tweet_media_path = match get_tweet_media(&media, &cache_path).await {
|
||||
Ok(l) => l,
|
||||
Err(e) => {
|
||||
return Err(ScootalooError::new(&format!(
|
||||
"Cannot get tweet media for {}: {}",
|
||||
&media.url, e
|
||||
)))
|
||||
}
|
||||
};
|
||||
let local_tweet_media_path = get_tweet_media(&media, &cache_path).await?;
|
||||
|
||||
// upload media to Mastodon
|
||||
let mastodon_media = mastodon.media(Cow::from(local_tweet_media_path.to_owned()));
|
||||
let mastodon_media =
|
||||
mastodon.media(Cow::from(local_tweet_media_path.to_owned()))?;
|
||||
// at this point, we can safely erase the original file
|
||||
// it doesn’t matter if we can’t remove, cache_media fn is idempotent
|
||||
remove_file(&local_tweet_media_path).await.ok();
|
||||
|
||||
let mastodon_media = match mastodon_media {
|
||||
Ok(m) => m,
|
||||
Err(e) => {
|
||||
return Err(ScootalooError::new(&format!(
|
||||
"Attachment {} cannot be uploaded to Mastodon Instance: {}",
|
||||
&local_tweet_media_path, e
|
||||
)))
|
||||
}
|
||||
};
|
||||
|
||||
Ok((i, mastodon_media.id))
|
||||
Ok::<(usize, String), ScootalooError>((i, mastodon_media.id))
|
||||
});
|
||||
|
||||
tasks.push(task);
|
||||
@@ -81,7 +64,7 @@ pub async fn generate_media_ids(
|
||||
match task.await {
|
||||
// insert the media at the right place
|
||||
Ok(Ok((i, v))) => media_ids[i] = v,
|
||||
Ok(Err(e)) => warn!("{}", e),
|
||||
Ok(Err(e)) => warn!("Cannot treat media: {}", e),
|
||||
Err(e) => error!("Something went wrong when joining the main thread: {}", e),
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user