From e3ba32f578e080a4c322ed91c3aba3c2abdd8568 Mon Sep 17 00:00:00 2001 From: Dev Goldy Date: Sun, 1 Oct 2023 00:24:34 +0100 Subject: [PATCH] replaced api error panic with AGHPBError --- Cargo.toml | 2 +- src/client.rs | 31 +++++++++++++++++++++++-------- src/lib.rs | 6 ++---- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 476d744..0e70ba9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,7 @@ readme = "README.md" documentation = "https://docs.rs/aghpb" repository = "https://github.com/THEGOLDENPRO/aghpb.rs" edition = "2021" -version = "1.3.0" +version = "1.3.2" [dependencies] reqwest = "0.11.18" diff --git a/src/client.rs b/src/client.rs index 096b666..69aef3d 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,4 +1,4 @@ -use std::collections::HashMap; +use std::{collections::HashMap, error::Error, fmt}; use chrono::DateTime; use reqwest::header::HeaderMap; @@ -12,6 +12,20 @@ pub struct Client { client: reqwest::Client } +#[derive(Debug)] +struct AGHPBError { + error: String, + message: String +} + +impl Error for AGHPBError {} + +impl fmt::Display for AGHPBError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "API Error: [{}] {}", self.error, self.message) + } +} + impl Client { pub fn new(api_url: Option<&str>) -> Self { Self { @@ -25,7 +39,7 @@ impl Client { /// WARNING: Will panic on incorrect category. /// /// Uses the ``/v1/random`` endpoint. - pub async fn random(&self, category: Option<&str>) -> Result { + pub async fn random(&self, category: Option<&str>) -> Result> { let mut queries: Vec<(&str, &str)> = Vec::new(); if let Some(category) = category { @@ -40,7 +54,13 @@ impl Client { Ok(get_book(headers, bytes)) } else { - Err(panic_on_api_error(&response.text().await?)) + let error_json: HashMap = serde_json::from_str(&response.text().await?).unwrap(); + Err( + AGHPBError { + error: error_json.get("error").unwrap().to_string(), + message: error_json.get("message").unwrap().to_string() + }.into() + ) } } @@ -81,9 +101,4 @@ fn get_book(headers: HeaderMap, bytes: Bytes) -> Book { date_added, raw_bytes: bytes } -} - -fn panic_on_api_error(text: &String) -> reqwest::Error { - let error_json: HashMap = serde_json::from_str(text).unwrap(); - panic!("API Error: {:?}", error_json.get("message").unwrap()); } \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index c3985d8..339a965 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -47,7 +47,7 @@ pub mod book; pub use book::*; pub use client::*; -use std::sync::OnceLock; +use std::{sync::OnceLock, error::Error}; static _CLIENT: OnceLock = OnceLock::new(); @@ -65,14 +65,12 @@ fn get_client() -> Client { /// Asynchronously grabs a random anime girl holding a programming book. /// -/// WARNING: Will panic on incorrect category. -/// /// NOTE: Use aghpb::Client for multiple requests. This uses a global client! /// If you want more customization/speed it maybe preferable to make /// your own client. /// /// Uses the ``/v1/random`` endpoint. -pub async fn random(category: Option<&str>) -> Result { +pub async fn random(category: Option<&str>) -> Result> { get_client().random(category).await }