From 5c5b37b70d1eb218664dbd20b44990919a9febfa Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Fri, 1 May 2026 20:39:46 +0300 Subject: [PATCH] platform: handle platform API responses Signed-off-by: NotAShelf Change-Id: Iedf8e6d6ecf80a42c24a000f6c787ff56a6a6964 --- src/platform/curseforge.rs | 26 +++++++++++++++++++++++--- src/platform/multiplatform.rs | 4 +--- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/platform/curseforge.rs b/src/platform/curseforge.rs index 6629045..692f14d 100644 --- a/src/platform/curseforge.rs +++ b/src/platform/curseforge.rs @@ -58,6 +58,25 @@ impl CurseForgePlatform { Ok(headers) } + /// Map a non-success HTTP status to the appropriate `PakkerError`. + fn map_http_error(status: reqwest::StatusCode, context: &str) -> PakkerError { + match status.as_u16() { + 404 => PakkerError::ProjectNotFound(context.to_string()), + 401 | 403 => { + PakkerError::ConfigError(format!( + "CurseForge API authentication failed ({}). Check your API key.", + status + )) + }, + _ => { + PakkerError::PlatformApiError(format!( + "CurseForge API returned {} for {}", + status, context + )) + }, + } + } + const fn map_class_id(class_id: u32) -> ProjectType { match class_id { 12 => ProjectType::ResourcePack, @@ -223,7 +242,7 @@ impl CurseForgePlatform { .await?; if !response.status().is_success() { - return Err(PakkerError::ProjectNotFound(slug.to_string())); + return Err(Self::map_http_error(response.status(), slug)); } let result: CurseForgeSearchResponse = response.json().await?; @@ -258,6 +277,7 @@ impl PlatformClient for CurseForgePlatform { let result: CurseForgeProjectResponse = response.json().await?; return Ok(Self::convert_project(result.data)); } + return Err(Self::map_http_error(response.status(), identifier)); } let cf_project = self.search_project_by_slug(identifier).await?; @@ -308,7 +328,7 @@ impl PlatformClient for CurseForgePlatform { .await?; if !response.status().is_success() { - return Err(PakkerError::ProjectNotFound(project_id.to_string())); + return Err(Self::map_http_error(response.status(), project_id)); } let result: CurseForgeFilesResponse = response.json().await?; @@ -364,7 +384,7 @@ impl PlatformClient for CurseForgePlatform { .await?; if !response.status().is_success() { - return Ok(None); + return Err(Self::map_http_error(response.status(), "lookup_by_hash")); } let response_data: serde_json::Value = response.json().await?; diff --git a/src/platform/multiplatform.rs b/src/platform/multiplatform.rs index 2fda754..54dac2d 100644 --- a/src/platform/multiplatform.rs +++ b/src/platform/multiplatform.rs @@ -38,9 +38,7 @@ impl MultiplatformPlatform { Err(e) => { let is_not_found = matches!( e, - PakkerError::ProjectNotFound(_) - | PakkerError::InvalidResponse(_) - | PakkerError::ConfigError(_) + PakkerError::ProjectNotFound(_) | PakkerError::InvalidResponse(_) ); if is_not_found { Ok(None) } else { Err(e) } },