Skip to content

Commit

Permalink
replaced api error panic with AGHPBError
Browse files Browse the repository at this point in the history
  • Loading branch information
THEGOLDENPRO committed Sep 30, 2023
1 parent ca99632 commit e3ba32f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
31 changes: 23 additions & 8 deletions src/client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::{collections::HashMap, error::Error, fmt};

use chrono::DateTime;
use reqwest::header::HeaderMap;
Expand All @@ -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 {
Expand All @@ -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<Book, reqwest::Error> {
pub async fn random(&self, category: Option<&str>) -> Result<Book, Box<dyn Error>> {
let mut queries: Vec<(&str, &str)> = Vec::new();

if let Some(category) = category {
Expand All @@ -40,7 +54,13 @@ impl Client {

Ok(get_book(headers, bytes))
} else {
Err(panic_on_api_error(&response.text().await?))
let error_json: HashMap<String, String> = 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()
)
}

}
Expand Down Expand Up @@ -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<String, String> = serde_json::from_str(text).unwrap();
panic!("API Error: {:?}", error_json.get("message").unwrap());
}
6 changes: 2 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Client> = OnceLock::new();

Expand All @@ -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<Book, reqwest::Error> {
pub async fn random(category: Option<&str>) -> Result<Book, Box<dyn Error>> {
get_client().random(category).await
}

Expand Down

0 comments on commit e3ba32f

Please sign in to comment.