Overhaul Error handling (Option instead of Result<T, ()> + Logging changes

This commit is contained in:
Neshura 2024-05-07 23:49:55 +02:00
parent 5d708bdb82
commit b6f5c38e4a
Signed by: Neshura
GPG key ID: B6983AAA6B9A7A6C
5 changed files with 146 additions and 96 deletions

View file

@ -11,6 +11,15 @@ use systemd_journal_logger::connected_to_journal;
use crate::fetchers::{FetcherTrait, Fetcher};
use crate::fetchers::jnovel::{JNovelFetcher};
macro_rules! debug {
($msg:tt) => {
match connected_to_journal() {
true => log::debug!("[DEBUG] {}", $msg),
false => println!("[DEBUG] {}", $msg),
}
};
}
macro_rules! info {
($msg:tt) => {
match connected_to_journal() {
@ -168,8 +177,8 @@ impl SeriesConfig {
info!(info);
let post_id = match lemmy.post(post_data).await {
Ok(data) => data,
Err(_) => {
Some(data) => data,
None=> {
error!("Error posting chapter");
return;
}
@ -188,31 +197,18 @@ impl SeriesConfig {
post_info.get_post_config(self).name.as_str()
);
info!(info);
let pinned_posts = match lemmy.get_community_pinned(lemmy.get_community_id(&post_info.get_post_config(self).name)).await {
Ok(data) => data,
Err(_) => {
error!("Pinning of Post to community failed");
continue;
}
};
let pinned_posts = lemmy.get_community_pinned(lemmy.get_community_id(&post_info.get_post_config(self).name)).await.unwrap_or_else(|| {
error!("Pinning of Post to community failed");
vec![]
});
if !pinned_posts.is_empty() {
let community_pinned_post = &pinned_posts[0];
match lemmy
.unpin(community_pinned_post.post.id, PostFeatureType::Community)
.await {
Ok(_) => {}
Err(_) => {
error!("Error un-pinning post");
return;
}
if lemmy.unpin(community_pinned_post.post.id, PostFeatureType::Community).await.is_none() {
error!("Error un-pinning post");
}
}
match lemmy.pin(post_id, PostFeatureType::Community).await {
Ok(_) => {}
Err(_) => {
error!("Error pinning post");
return;
}
if lemmy.pin(post_id, PostFeatureType::Community).await.is_none() {
error!("Error pinning post");
}
} else if read_config
.protected_communities
@ -229,10 +225,10 @@ impl SeriesConfig {
let info = format!("Pinning '{}' to Instance", post_info.get_info().title);
info!(info);
let pinned_posts = match lemmy.get_local_pinned().await {
Ok(data) => {data}
Err(_) => {
Some(data) => {data}
None => {
error!("Error fetching pinned posts");
return;
vec![]
}
};
@ -245,25 +241,16 @@ impl SeriesConfig {
continue;
} else {
let community_pinned_post = &pinned_post;
match lemmy
.unpin(community_pinned_post.post.id, PostFeatureType::Local)
.await {
Ok(_) => {}
Err(_) => {
error!("Error pinning post");
return;
}
if lemmy.unpin(community_pinned_post.post.id, PostFeatureType::Local).await.is_none() {
error!("Error pinning post");
continue;
}
break;
}
}
}
match lemmy.pin(post_id, PostFeatureType::Local).await {
Ok(_) => {}
Err(_) => {
error!("Error pinning post");
return;
}
if lemmy.pin(post_id, PostFeatureType::Local).await.is_none() {
error!("Error pinning post");
};
}
@ -283,6 +270,7 @@ impl SeriesConfig {
series_history.set_part(post_info.get_part_info().unwrap_or(PartInfo::NoParts).as_string().as_str(), part_history);
history
.set_series(self.slug.as_str(), series_history);
debug!("Saving History");
history.save_history();
}
}