From ecc4743fdec43eb578e9c35bb008c68909f1517e Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Wed, 4 Sep 2024 17:32:19 +0200 Subject: better error handling --- src/commands/modification.rs | 63 ++++++++++++++++++++------------------------ 1 file changed, 28 insertions(+), 35 deletions(-) (limited to 'src/commands/modification.rs') diff --git a/src/commands/modification.rs b/src/commands/modification.rs index d20f575..27ba098 100644 --- a/src/commands/modification.rs +++ b/src/commands/modification.rs @@ -3,11 +3,23 @@ use std::collections::HashMap; use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; use crate::{ - apis::modrinth::{extract_current_version, get_raw_versions, project, projects, versions, Version}, config::Cfg, data::{modification::{AddMod, IDSelector}, project::ProjectInfo}, db::{ + apis::modrinth::{ + extract_current_version, get_raw_versions, project, projects, versions, + Version, + }, + config::Cfg, + data::{ + modification::{AddMod, IDSelector}, + projectinfo::ProjectInfo, + }, + db::{ lists_get_all_ids, mods_get_id, mods_get_info, mods_insert, mods_remove, userlist_get_all_ids, userlist_get_current_version, userlist_insert, userlist_remove, - }, error::{EType, MLErr, MLE}, files::{delete_version, download_versions}, List, PROGRESS_CHARS, STYLE_BAR_POS, STYLE_OPERATION + }, + errors::{Error, MLE}, + files::{delete_version, download_versions}, + List, PROGRESS_CHARS, STYLE_BAR_POS, STYLE_OPERATION, }; /// # Errors @@ -22,14 +34,9 @@ pub async fn mod_add( let mut mod_ids: Vec<(String, bool)> = Vec::new(); let mut ver_ids: Vec<(String, bool)> = Vec::new(); - let add_p = mp.add(ProgressBar::new( - mods.len() - .try_into() - .map_err(|_| MLErr::new(EType::Other, "MODSLENTRY"))?, - )); + let add_p = mp.add(ProgressBar::new(mods.len().try_into()?)); add_p.set_style( - ProgressStyle::with_template(STYLE_BAR_POS) - .map_err(|_| MLErr::new(EType::LibIndicatif, "template error"))? + ProgressStyle::with_template(STYLE_BAR_POS)? .progress_chars(PROGRESS_CHARS), ); add_p.set_message("Sort ids"); @@ -57,7 +64,7 @@ pub async fn mod_add( }; if projectinfo.is_empty() { - return Err(MLErr::new(EType::ArgumentError, "NO_IDS?")); + return Err(Error::NoProjectInfo); }; add_p.set_message("Add mods to database"); @@ -65,29 +72,18 @@ pub async fn mod_add( let mut downloadstack: Vec = Vec::new(); //Adding each mod to the lists and downloadstack - let project_p = mp.insert_before( - &add_p, - ProgressBar::new( - projectinfo - .len() - .try_into() - .map_err(|_| MLErr::new(EType::Other, "infolen"))?, - ), - ); + let project_p = mp + .insert_before(&add_p, ProgressBar::new(projectinfo.len().try_into()?)); project_p.set_style( - ProgressStyle::with_template(STYLE_BAR_POS) - .map_err(|_| MLErr::new(EType::LibIndicatif, "template error"))? + ProgressStyle::with_template(STYLE_BAR_POS)? .progress_chars(PROGRESS_CHARS), ); for project in projectinfo { add_project(config, &project_p, &project, &list)?; if project.current_version.is_some() { - downloadstack.push( - project - .current_version - .ok_or(MLErr::new(EType::Other, "cur_ver"))?, - ); + downloadstack + .push(project.current_version.ok_or(Error::NoCurrentVersion)?); }; } @@ -125,7 +121,7 @@ fn add_project( project .current_version .clone() - .ok_or(MLErr::new(EType::Other, "cur_ver"))? + .ok_or(Error::NoCurrentVersion)? .id }; @@ -142,9 +138,9 @@ fn add_project( let expected_err = format!("SQL: UNIQUE constraint failed: {}.mod_id", list.id); if e.to_string() == expected_err { - Err(MLErr::new(EType::ModError, "MOD_ALREADY_ON_SELECTED_LIST")) + Err(Error::ModAlreadyOnList) } else { - Err(e) + Err(e)? } } Ok(..) => Ok(..), @@ -212,7 +208,7 @@ async fn get_mod_infos( download_link: file, set_version: *setmap .get(&project.id) - .ok_or(MLErr::new(EType::Other, "not in setmap"))?, + .ok_or(Error::VersionSetNotSet)?, }); } else { let current_id = @@ -312,10 +308,7 @@ async fn get_ver_info( /// # Errors pub fn mod_remove(config: &Cfg, id: &str, list: &List) -> MLE<()> { let progress = ProgressBar::new_spinner(); - progress.set_style( - ProgressStyle::with_template(STYLE_OPERATION) - .map_err(|_| MLErr::new(EType::LibIndicatif, "template error"))?, - ); + progress.set_style(ProgressStyle::with_template(STYLE_OPERATION)?); let mod_id = mods_get_id(&config.data, id)?; @@ -351,7 +344,7 @@ pub fn mod_remove(config: &Cfg, id: &str, list: &List) -> MLE<()> { if err.to_string() == "Database: NO_MODS_USERLIST" { return Ok(()); }; - return Err(err); + return Err(err)?; } }; if mods.contains(&mod_id) { -- cgit v1.2.3