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/db.rs | 151 ++++++++++++++++++++++++++++++++------------------------------ 1 file changed, 78 insertions(+), 73 deletions(-) (limited to 'src/db.rs') diff --git a/src/db.rs b/src/db.rs index 168cbbe..003c98e 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1,16 +1,39 @@ -use std::io::{Error, ErrorKind}; - use rusqlite::Connection; use crate::{ config::Cfg, data::{list::List, modloader::Modloader}, - error::{EType, MLErr, MLE}, }; +type DbE = Result; + +#[derive(Debug, thiserror::Error)] +pub enum DBError { + #[error("sqlite: {source}")] + Sqlite { + #[from] + source: rusqlite::Error, + }, + + #[error("conversion: {source}")] + Conversion { + #[from] + source: crate::errors::ConversionError, + }, + + #[error("not enough arguments")] + NotEnoughArguments, + + #[error("couldn't find requested mod/version")] + NotFound, + + #[error("no mods/versions are available in the requested scope")] + NotAvailable, +} + //MODS /// # Errors -pub fn mods_insert(config: &Cfg, id: &str, slug: &str, name: &str) -> MLE<()> { +pub fn mods_insert(config: &Cfg, id: &str, slug: &str, name: &str) -> DbE<()> { let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; @@ -23,9 +46,7 @@ pub fn mods_insert(config: &Cfg, id: &str, slug: &str, name: &str) -> MLE<()> { } /// # Errors -pub fn mods_get_all_ids( - config: &Cfg, -) -> Result, Box> { +pub fn mods_get_all_ids(config: &Cfg) -> DbE> { let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; @@ -39,7 +60,7 @@ pub fn mods_get_all_ids( } if mods.is_empty() { - Err(Box::new(Error::new(ErrorKind::NotFound, "NO_MODS_ALL"))) + Err(DBError::NotFound) } else { Ok(mods) } @@ -53,8 +74,8 @@ pub fn mods_get_all_ids( /// /// # Errors /// -/// Will return `MLError` when no mod id is found -pub fn mods_get_id(data: &str, slug: &str) -> MLE { +/// Will return `DBError` when no mod id is found +pub fn mods_get_id(data: &str, slug: &str) -> DbE { let data = format!("{data}/data.db"); let connection = Connection::open(data)?; @@ -91,7 +112,7 @@ pub fn mods_get_id(data: &str, slug: &str) -> MLE { } if mod_id.is_empty() { - return Err(MLErr::new(EType::DBError, "GI_MOD_NOT_FOUND")); + Err(DBError::NotFound)?; }; Ok(mod_id) @@ -103,7 +124,7 @@ pub struct ModInfo { } /// # Errors -pub fn mods_get_info(config: &Cfg, id: &str) -> MLE { +pub fn mods_get_info(config: &Cfg, id: &str) -> DbE { let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; @@ -125,15 +146,15 @@ pub fn mods_get_info(config: &Cfg, id: &str) -> MLE { }); } - if mod_info.is_none() { - Err(MLErr::new(EType::DBError, "GN_MOD_NOT_FOUND")) + if let Some(mi) = mod_info { + Ok(mi) } else { - Ok(mod_info.ok_or(MLErr::new(EType::Other, "mod_info"))?) + Err(DBError::NotFound)? } } /// # Errors -pub fn mods_remove(config: &Cfg, id: &str) -> MLE<()> { +pub fn mods_remove(config: &Cfg, id: &str) -> DbE<()> { let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; @@ -152,12 +173,12 @@ pub struct DBModlistVersions { pub fn mods_get_versions( config: &Cfg, mods: &[String], -) -> MLE> { +) -> DbE> { let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; if mods.is_empty() { - return Err(MLErr::new(EType::ArgumentError, "MODS_NO_INPUT")); + Err(DBError::NotEnoughArguments)?; } let mut wherestr = String::from("WHERE"); @@ -190,7 +211,7 @@ pub fn mods_get_versions( } if versionmaps.is_empty() { - Err(MLErr::new(EType::DBError, "MODS_MODS_NOT_FOUND")) + Err(DBError::NotFound) } else { Ok(versionmaps) } @@ -206,7 +227,7 @@ pub fn userlist_insert( applicable_versions: &[String], current_link: &str, set_version: bool, -) -> MLE<()> { +) -> DbE<()> { let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; @@ -228,7 +249,7 @@ pub fn userlist_insert( } /// # Errors -pub fn userlist_get_all_ids(config: &Cfg, list_id: &str) -> MLE> { +pub fn userlist_get_all_ids(config: &Cfg, list_id: &str) -> DbE> { let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; @@ -242,17 +263,14 @@ pub fn userlist_get_all_ids(config: &Cfg, list_id: &str) -> MLE> { } if mod_ids.is_empty() { - Err(MLErr::new( - EType::DBError, - &format!("NO_MODS_USERLIST{list_id}"), - )) + Err(DBError::NotAvailable) } else { Ok(mod_ids) } } /// # Errors -pub fn userlist_remove(config: &Cfg, list_id: &str, mod_id: &str) -> MLE<()> { +pub fn userlist_remove(config: &Cfg, list_id: &str, mod_id: &str) -> DbE<()> { let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; @@ -268,7 +286,7 @@ pub fn userlist_get_applicable_versions( config: &Cfg, list_id: &str, mod_id: String, -) -> MLE { +) -> DbE { let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; @@ -285,7 +303,7 @@ pub fn userlist_get_applicable_versions( } if version.is_empty() { - Err(MLErr::new(EType::DBError, "GAV_MOD_NOT_FOUND")) + Err(DBError::NotFound) } else { Ok(version) } @@ -295,7 +313,7 @@ pub fn userlist_get_applicable_versions( pub fn userlist_get_all_applicable_versions_with_mods( config: &Cfg, list_id: &str, -) -> MLE> { +) -> DbE> { let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; @@ -316,7 +334,7 @@ pub fn userlist_get_all_applicable_versions_with_mods( } if versions.is_empty() { - return Err(MLErr::new(EType::DBError, "NO_MODS_ON_LIST")); + return Err(DBError::NotAvailable); }; Ok(versions) @@ -327,7 +345,7 @@ pub fn userlist_get_current_version( config: &Cfg, list_id: &str, mod_id: &str, -) -> MLE { +) -> DbE { let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; @@ -344,7 +362,7 @@ pub fn userlist_get_current_version( } if version.is_empty() { - Err(MLErr::new(EType::DBError, "GCV_MOD_NOT_FOUND")) + Err(DBError::NotFound) } else { Ok(version) } @@ -354,7 +372,7 @@ pub fn userlist_get_current_version( pub fn userlist_get_all_current_version_ids( config: &Cfg, list_id: &str, -) -> MLE> { +) -> DbE> { let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; @@ -368,7 +386,7 @@ pub fn userlist_get_all_current_version_ids( } if versions.is_empty() { - return Err(MLErr::new(EType::DBError, "NO_MODS_ON_LIST")); + return Err(DBError::NotAvailable); }; Ok(versions) @@ -378,7 +396,7 @@ pub fn userlist_get_all_current_version_ids( pub fn userlist_get_all_current_versions_with_mods( config: &Cfg, list_id: &str, -) -> Result, Box> { +) -> DbE> { let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; @@ -399,10 +417,7 @@ pub fn userlist_get_all_current_versions_with_mods( } if versions.is_empty() { - return Err(Box::new(std::io::Error::new( - ErrorKind::Other, - "NO_MODS_ON_LIST", - ))); + return Err(DBError::NotAvailable); }; Ok(versions) @@ -413,7 +428,7 @@ pub fn userlist_get_set_version( config: &Cfg, list_id: &str, mod_id: &str, -) -> MLE { +) -> DbE { let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; @@ -439,7 +454,7 @@ pub fn userlist_change_versions( versions: String, link: String, mod_id: String, -) -> MLE<()> { +) -> DbE<()> { let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; @@ -453,7 +468,7 @@ pub fn userlist_add_disabled_versions( list_id: &str, disabled_version: String, mod_id: String, -) -> MLE<()> { +) -> DbE<()> { let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; @@ -480,7 +495,7 @@ pub fn userlist_get_disabled_versions( config: &Cfg, list_id: &str, mod_id: String, -) -> MLE { +) -> DbE { let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; @@ -497,7 +512,7 @@ pub fn userlist_get_disabled_versions( } if version.is_empty() { - Err(MLErr::new(EType::DBError, "GDV_MOD_NOT_FOUND")) + Err(DBError::NotFound) } else { Ok(version) } @@ -507,7 +522,7 @@ pub fn userlist_get_disabled_versions( pub fn userlist_get_all_downloads( config: &Cfg, list_id: &str, -) -> Result, Box> { +) -> DbE> { let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; @@ -522,10 +537,7 @@ pub fn userlist_get_all_downloads( } if links.is_empty() { - return Err(Box::new(std::io::Error::new( - ErrorKind::Other, - "NO_MODS_ON_LIST", - ))); + return Err(DBError::NotAvailable); }; Ok(links) @@ -540,7 +552,7 @@ pub fn lists_insert( mc_version: &str, mod_loader: &Modloader, download_folder: &str, -) -> MLE<()> { +) -> DbE<()> { let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; @@ -554,7 +566,7 @@ pub fn lists_insert( } /// # Errors -pub fn lists_remove(config: &Cfg, id: &str) -> MLE<()> { +pub fn lists_remove(config: &Cfg, id: &str) -> DbE<()> { let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; @@ -564,7 +576,7 @@ pub fn lists_remove(config: &Cfg, id: &str) -> MLE<()> { } /// # Errors -pub fn lists_get(config: &Cfg, list_id: &str) -> MLE { +pub fn lists_get(config: &Cfg, list_id: &str) -> DbE { let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; @@ -591,20 +603,20 @@ pub fn lists_get(config: &Cfg, list_id: &str) -> MLE { list = List { id: list_id.to_string(), mc_version: String::from(&li[0]), - modloader: Modloader::from(&li[1])?, + modloader: Modloader::try_from(li[1].as_str())?, download_folder: String::from(&li[2]), }; } if list.id.is_empty() { - return Err(MLErr::new(EType::DBError, "LIST_NOT_FOUND")); + Err(DBError::NotFound)?; } Ok(list) } /// # Errors -pub fn lists_version(config: &Cfg, list_id: &str, version: &str) -> MLE<()> { +pub fn lists_version(config: &Cfg, list_id: &str, version: &str) -> DbE<()> { let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; @@ -616,7 +628,7 @@ pub fn lists_version(config: &Cfg, list_id: &str, version: &str) -> MLE<()> { } /// # Errors -pub fn lists_get_all_ids(config: &Cfg) -> MLE> { +pub fn lists_get_all_ids(config: &Cfg) -> DbE> { let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; @@ -629,7 +641,7 @@ pub fn lists_get_all_ids(config: &Cfg) -> MLE> { } if list_ids.is_empty() { - Err(MLErr::new(EType::DBError, "NO_LISTS")) + Err(DBError::NotAvailable) } else { Ok(list_ids) } @@ -637,7 +649,7 @@ pub fn lists_get_all_ids(config: &Cfg) -> MLE> { //config /// # Errors -pub fn config_change_current_list(config: &Cfg, id: &str) -> MLE<()> { +pub fn config_change_current_list(config: &Cfg, id: &str) -> DbE<()> { let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; @@ -649,7 +661,7 @@ pub fn config_change_current_list(config: &Cfg, id: &str) -> MLE<()> { } /// # Errors -pub fn config_get_current_list(config: &Cfg) -> MLE { +pub fn config_get_current_list(config: &Cfg) -> DbE { let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; @@ -663,7 +675,7 @@ pub fn config_get_current_list(config: &Cfg) -> MLE { } if list_id.is_empty() { - return Err(MLErr::new(EType::DBError, "NO_CURRENT_LIST")); + return Err(DBError::NotAvailable); } Ok(list_id) @@ -720,7 +732,7 @@ pub fn s_config_update_version( /// # Errors pub fn s_config_get_version( config: &Cfg, -) -> Result> { +) -> DbE { let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; @@ -734,10 +746,7 @@ pub fn s_config_get_version( } if version.is_empty() { - return Err(Box::new(std::io::Error::new( - ErrorKind::Other, - "NO_DBVERSION", - ))); + return Err(DBError::NotAvailable); }; Ok(version) } @@ -749,18 +758,14 @@ pub fn s_insert_column( column: &str, c_type: &str, default: Option, -) -> Result<(), Box> { +) -> DbE<()> { let data = format!("{}/data.db", config.data); let connection = Connection::open(data)?; let mut sql = format!("ALTER TABLE {table} ADD '{column}' {c_type}"); - if default.is_some() { - sql = format!( - "{} DEFAULT {}", - sql, - default.ok_or(MLErr::new(EType::Other, "errornous default"))? - ); + if let Some(def) = default { + sql = format!("{sql} DEFAULT {def}"); } connection.execute(sql.as_str(), ())?; @@ -768,7 +773,7 @@ pub fn s_insert_column( } /// # Errors -pub fn setup(path: &str) -> MLE<()> { +pub fn setup(path: &str) -> DbE<()> { let connection = Connection::open(path)?; connection.execute_batch( -- cgit v1.2.3