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/apis/modrinth.rs | 51 +++++++++++++++++++-------------------------------- 1 file changed, 19 insertions(+), 32 deletions(-) (limited to 'src/apis/modrinth.rs') diff --git a/src/apis/modrinth.rs b/src/apis/modrinth.rs index 7f1fb52..afd7b69 100644 --- a/src/apis/modrinth.rs +++ b/src/apis/modrinth.rs @@ -3,7 +3,7 @@ use reqwest::Client; use serde::{Deserialize, Serialize}; use crate::{ - error::{EType, MLErr, MLE}, + errors::{Error, MLE}, List, }; @@ -131,10 +131,7 @@ pub enum GameVersionType { } /// # Errors -async fn get( - api: &str, - path: &str, -) -> Result>, Box> { +async fn get(api: &str, path: &str) -> Result, Error> { let url = format!(r#"{api}{path}"#); let client = Client::builder() @@ -145,23 +142,19 @@ async fn get( .build()?; let res = client.get(url).send().await?; - let mut data: Option> = None; - - if res.status() == 200 { - data = Some(res.bytes().await?.to_vec()); + if res.status() != 200 { + return Err(Error::RequestNotOK(res.status())); } - Ok(data) + Ok(res.bytes().await?.to_vec()) } /// # Errors pub async fn project(api: &str, name: &str) -> MLE { let url = format!("project/{name}"); - let data = get(api, &url).await - .map_err(|_| MLErr::new(EType::Other, "geterr"))? - .ok_or(MLErr::new(EType::Other, "geterr2"))?; + let data = get(api, &url).await?; - serde_json::from_slice(&data).map_err(|_| MLErr::new(EType::LibJson, "from project")) + Ok(serde_json::from_slice(&data)?) } /// # Errors @@ -169,11 +162,9 @@ pub async fn projects(api: &str, ids: Vec) -> MLE> { let all = ids.join(r#"",""#); let url = format!(r#"projects?ids=["{all}"]"#); - let data = get(api, &url).await - .map_err(|_| MLErr::new(EType::Other, "geterr"))? - .ok_or(MLErr::new(EType::Other, "geterr2"))?; + let data = get(api, &url).await?; - serde_json::from_slice(&data).map_err(|_| MLErr::new(EType::LibJson, "from projects")) + Ok(serde_json::from_slice(&data)?) } ///Get applicable versions from `mod_id` with list context @@ -184,12 +175,12 @@ pub async fn versions(api: &str, id: String, list: List) -> MLE> { id, list.modloader, list.mc_version ); - let data = get(api, &url).await - .map_err(|_| MLErr::new(EType::Other, "geterr"))?; + let data = get(api, &url).await?; - Ok(match data { - Some(data) => serde_json::from_slice(&data).map_err(|_| MLErr::new(EType::LibJson, "from version"))?, - None => Vec::new(), + Ok(if data.is_empty() { + Vec::new() + } else { + serde_json::from_slice(&data)? }) } @@ -201,17 +192,15 @@ pub async fn get_raw_versions( ) -> MLE> { let url = format!(r#"versions?ids=["{}"]"#, versions.join(r#"",""#)); - let data = get(api, &url).await - .map_err(|_| MLErr::new(EType::Other, "geterr"))? - .ok_or(MLErr::new(EType::Other, "geterr2"))?; + let data = get(api, &url).await?; - serde_json::from_slice(&data).map_err(|_| MLErr::new(EType::LibJson, "from raw version")) + Ok(serde_json::from_slice(&data)?) } /// # Errors pub fn extract_current_version(versions: Vec) -> MLE { match versions.len() { - 0 => Err(MLErr::new(EType::ModError, "NO_VERSIONS_AVAILABLE")), + 0 => Err(Error::ModError("NO_VERSIONS_AVAILABLE".to_string())), 1.. => { let mut times: Vec<(String, DateTime)> = vec![]; for ver in versions { @@ -228,9 +217,7 @@ pub fn extract_current_version(versions: Vec) -> MLE { /// # Errors pub async fn get_game_versions() -> MLE> { let data = get("https://api.modrinth.com/v2/", "tag/game_version") - .await - .map_err(|_| MLErr::new(EType::Other, "geterr"))? - .ok_or(MLErr::new(EType::Other, "geterr2"))?; + .await?; - serde_json::from_slice(&data).map_err(|_| MLErr::new(EType::LibJson, "from game version")) + Ok(serde_json::from_slice(&data)?) } -- cgit v1.2.3