refactor: simpler error bubbling inside async block

This commit is contained in:
VC
2022-11-14 14:26:25 +01:00
parent 9c14636735
commit 78924f6eeb

View File

@@ -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 doesnt matter if we cant 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),
}
}