From 81d7282e8a6a7ba2e6d7baa0c6c138839f733eed Mon Sep 17 00:00:00 2001 From: murmeldin Date: Thu, 12 Dec 2024 14:06:09 +0100 Subject: [PATCH] proper error handling for mediawiki edits --- src/mediawiki.rs | 47 +++++++++++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/src/mediawiki.rs b/src/mediawiki.rs index b7b761a..416333d 100644 --- a/src/mediawiki.rs +++ b/src/mediawiki.rs @@ -1,5 +1,6 @@ use std::cell::OnceCell; use std::error::Error; +use std::f64::consts::E; use chrono::{Datelike, NaiveDate, Utc}; use colored::Colorize; @@ -75,13 +76,13 @@ impl std::fmt::Debug for Mediawiki { pub enum ValidRequestTypes { Get, Post, - PostForEditing + PostForEditing, } pub enum ValidPageEdits { WithPotentiallyOverriding, WithoutOverriding, - ModifyPlenumPageAfterwards_WithoutOverriding + ModifyPlenumPageAfterwards_WithoutOverriding, } impl Mediawiki { @@ -223,7 +224,6 @@ impl Mediawiki { resp } - /// Creates a completely new wiki page with page_content and page_title as inputs pub fn new_wiki_page( &self, page_title: &str, page_content: &str, update_main_page: ValidPageEdits, @@ -243,7 +243,7 @@ impl Mediawiki { } let url = format!("{}/api.php", self.server_url); - + let params: Box<[(&str, &str)]> = match update_main_page { ValidPageEdits::WithPotentiallyOverriding => { // This means we *EDIT* the *Main Page* and do not prevent overwriting @@ -253,9 +253,9 @@ impl Mediawiki { ("title", page_title), ("text", page_content), ("token", self.csrf_token.get().unwrap()), - ("bot", "true") - ]) - } + ("bot", "true"), + ]) + }, ValidPageEdits::ModifyPlenumPageAfterwards_WithoutOverriding => { // This means we *CREATE* a *new Page* and always prevent overwriting Box::from([ @@ -267,7 +267,7 @@ impl Mediawiki { ("createonly", "true"), // Prevent overwriting existing pages ("bot", "true"), ]) - } + }, ValidPageEdits::WithoutOverriding => { // This means we *CREATE* a *new Page* and always prevent overwriting Box::from([ @@ -279,7 +279,7 @@ impl Mediawiki { ("createonly", "true"), // Prevent overwriting existing pages ("bot", "true"), ]) - } + }, }; verboseln!("Current page title: {page_title}"); @@ -294,11 +294,19 @@ impl Mediawiki { let response_result = serde_json::from_str::(&request_result); let response = response_result.unwrap_or_else(|e| { print!("Error while creating new wiki page:\n{}", e.to_string().cyan()); - return serde_json::from_str("\"(error)\"").unwrap() + return serde_json::from_str("\"(error)\"").unwrap(); }); verboseln!("pos2"); // Check if the page creation was successful - if let Some(edit) = response.get("edit") { + if let Some(error) = response.get("error") { + if let Some(info) = error.get("info") { + if info == "The page you tried to create has been created already." { + verboseln!("The page you tried to create has been created already. Continuing...") + } else { + println!("There was an error while editing the wiki: {}", info.to_string().yellow()) + } + } + } else if let Some(edit) = response.get("edit") { verboseln!("pos3"); if edit.get("result").and_then(|r| r.as_str()) == Some("Success") { verboseln!("Successfully created wiki page: {}", page_title); @@ -318,9 +326,9 @@ impl Mediawiki { verboseln!("updating main page..."); self.update_plenum_page(page_title)? }, - _ => () + _ => (), }; - return Ok(page_title.to_string()) + return Ok(page_title.to_string()); } /// This function is responsible for updating the main plenum page: @@ -339,7 +347,6 @@ impl Mediawiki { // check first if the script has been run before and if the link already exists if !page_content.contains(&new_page_title_to_link_to) { - // check if the current year heading pattern exists if !page_content.contains(&year_heading_pattern) { // If not, add a new year heading pattern @@ -375,7 +382,11 @@ impl Mediawiki { verboseln!("{}", "The bot appears to have been run before and a duplicate link to the new plenum pad was avoided.".yellow()) } // refresh page - self.new_wiki_page(&self.plenum_main_page_name, &page_content, ValidPageEdits::WithPotentiallyOverriding)?; + self.new_wiki_page( + &self.plenum_main_page_name, + &page_content, + ValidPageEdits::WithPotentiallyOverriding, + )?; Ok(()) } /// This function downloads and returns the contents of a wiki page when given the page's title (e.g. `page_title = Plenum/13._August_2024`) @@ -512,7 +523,11 @@ pub fn pad_ins_wiki( let page_title = create_page_title(date); let full_page_title = format!("{}/{}", wiki.plenum_main_page_name, page_title); - wiki.new_wiki_page(&full_page_title, &pad_converted, ValidPageEdits::ModifyPlenumPageAfterwards_WithoutOverriding)?; + wiki.new_wiki_page( + &full_page_title, + &pad_converted, + ValidPageEdits::ModifyPlenumPageAfterwards_WithoutOverriding, + )?; verboseln!("Finished successfully with wiki"); Ok(())