From a549c89c33a8de08c7e983e86fc1cd6c749188f0 Mon Sep 17 00:00:00 2001 From: Neshura Date: Mon, 14 Aug 2023 22:52:34 +0200 Subject: [PATCH] Removed usage of loops to better detect where errors originate --- src/config/mod.rs | 6 +++--- src/main.rs | 45 +++++++++++++++++++-------------------------- 2 files changed, 22 insertions(+), 29 deletions(-) diff --git a/src/config/mod.rs b/src/config/mod.rs index 3ba8dfb..24994f2 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -1,4 +1,4 @@ -use std::{fs::{self, OpenOptions}, path::Path, io::Write, thread::sleep, time}; +use std::{fs::{self, OpenOptions}, path::Path, io::Write, thread::sleep, time, error::Error}; use lemmy_api_common::{sensitive::Sensitive, post::CreatePost, community::{ListCommunities, ListCommunitiesResponse}}; use lemmy_db_schema::{newtypes::{LanguageId, CommunityId, PostId}, ListingType}; @@ -98,7 +98,7 @@ impl Config { } pub(crate) fn check_feeds(&mut self, post_history: &mut Vec - , community_ids: &CommunitiesVector, auth: &Sensitive) -> Result, usize, String))>, reqwest::Error> { + , community_ids: &CommunitiesVector, auth: &Sensitive) -> Result, usize, String))>, Box> { let mut post_queue: Vec<(CreatePost, (Option, usize, String))> = vec![]; match self.feeds.iter().map(|feed| { @@ -267,7 +267,7 @@ impl CommunitiesVector { } #[warn(unused_results)] - pub(crate) fn load(&mut self, auth: &Sensitive, base: &String) -> Result<(), reqwest::Error> { + pub(crate) fn load(&mut self, auth: &Sensitive, base: &String) -> Result<(), Box> { let params = ListCommunities { auth: Some(auth.clone()), type_: Some(ListingType::Local), diff --git a/src/main.rs b/src/main.rs index edf3764..bf7e04d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,7 +10,7 @@ use lemmy_db_schema::{ }; use once_cell::sync::Lazy; use reqwest::{blocking::Client, StatusCode}; -use std::{thread::sleep, time, collections::HashMap}; +use std::{thread::sleep, time, collections::HashMap, error::Error}; mod config; @@ -50,7 +50,7 @@ impl Bot { /// /// * `return` : Returns true if token was succesfully retrieved, false otherwise #[warn(unused_results)] - pub(crate) fn login(&mut self) -> Result<(), reqwest::Error> { + pub(crate) fn login(&mut self) -> Result<(), Box> { let login_params = Login { username_or_email: self.secrets.lemmy.get_username(), password: self.secrets.lemmy.get_password(), @@ -71,7 +71,7 @@ impl Bot { return Ok(()); } else { println!("Error Code: {:?}", res.status()); - return Err(res.error_for_status().unwrap_err()); + return Err(Box::new(res.error_for_status().unwrap_err())); } } @@ -80,7 +80,7 @@ impl Bot { /// * `post_data` : Object of type [CreatePost] containing post info /// * `return` : Returns true if Post was succesful, false otherwise #[warn(unused_results)] - pub(crate) fn post(&mut self, post_data: CreatePost) -> Result { + pub(crate) fn post(&mut self, post_data: CreatePost) -> Result> { let res = CLIENT .post(self.config.instance.clone() + "/api/v3/post") .json(&post_data) @@ -92,7 +92,7 @@ impl Bot { } #[warn(unused_results)] - pub(crate) fn pin_new(&mut self, old_post: &Option, new_post: &PostView) -> Result<(), reqwest::Error> { + pub(crate) fn pin_new(&mut self, old_post: &Option, new_post: &PostView) -> Result<(), Box> { match old_post { Some(id) => { let remove_community_pin = FeaturePost { @@ -166,7 +166,7 @@ impl Bot { return Ok(()); } - pub(crate) fn pin (&mut self, pin_data: FeaturePost) -> Result { + pub(crate) fn pin (&mut self, pin_data: FeaturePost) -> Result> { let res = CLIENT .post(self.config.instance.clone() + "/api/v3/post/feature") .json(&pin_data) @@ -177,7 +177,7 @@ impl Bot { } #[warn(unused_results)] - pub(crate) fn run_once(&mut self, prev_time: &mut NaiveDateTime) -> Result<(), reqwest::Error> { + pub(crate) fn run_once(&mut self, prev_time: &mut NaiveDateTime) -> Result<(), Box> { println!("{:#<1$}", "", 30); self.start_time = Utc::now().naive_local(); @@ -195,24 +195,12 @@ impl Bot { let post_queue: Vec<(CreatePost, (Option, usize, String))> = self.config.check_feeds(&mut self.post_history, &self.community_ids, &self.auth)?; println!("Done!"); - post_queue.iter().for_each(|(post, (prev_idx, feed_id, feed_title))| { + let _ = post_queue.iter().map(|(post, (prev_idx, feed_id, feed_title))| -> Result<(), Box> { println!("Posting: {}", post.name); - let post_data: PostView; + let post_data = self.post(post.clone())?; - loop { - let post_res = self.post(post.clone()); - if post_res.is_ok() { - post_data = post_res.unwrap(); - break - }; - println!("Post attempt failed, retrying"); - } - - loop { - if self.pin_new(prev_idx, &post_data).is_ok() {break}; - println!("Pin attempt failed, retrying"); - } + self.pin_new(prev_idx, &post_data)?; // Move current post to old post list match prev_idx { @@ -228,7 +216,8 @@ impl Bot { last_post_url: post.url.clone().unwrap().to_string(), }), } - }); + Ok(()) + }).collect::>(); PrevPost::save(&self.post_history); @@ -296,15 +285,19 @@ fn run_bot() { loop { this.idle(); // 3 retries in case of connection issues - let mut loop_breaker: u8 = 0; - while !this.run_once(&mut old).is_ok() && loop_breaker <= 3 { + //let mut loop_breaker: u8 = 0; // DEBUG disabled for clearer crash finding + match this.run_once(&mut old) { + Ok(_) => {}, + Err(e) => panic!("{:#?}", e) + }; + /* while !this.run_once(&mut old).is_ok() && loop_breaker <= 3 { println!("Unable to complete Bot cycle, retrying with fresh login credentials"); if this.login().is_ok() { let _ = this.community_ids.load(&this.auth, &this.config.instance); } sleep(time::Duration::from_secs(10)); loop_breaker += 1; - }; + }; */ this.print_info(); this.idle(); }