Unify errors into one struct

This commit is contained in:
blankie 2023-10-28 11:49:35 +11:00
parent dd62cc2c19
commit 1d8210967c
Signed by: blankie
GPG Key ID: CC15FC822C7F61F5
2 changed files with 34 additions and 4 deletions

View File

@ -28,22 +28,22 @@ pub fn get_client() -> reqwest::Client {
pub async fn get_sauce_info(
client: reqwest::Client,
sauce: i32,
) -> Result<structs::GalleryInfo, reqwest::Error> {
) -> Result<structs::GalleryInfo, structs::Error> {
let mut uri = String::from("https://nhentai.net/api/gallery/");
uri.push_str(&sauce.to_string());
let resp = client.get(&uri).send().await?;
Ok(serde_json::from_str(&resp.text().await?).unwrap())
Ok(serde_json::from_str(&resp.text().await?)?)
}
pub async fn get_search_info(
client: reqwest::Client,
search_query: &str,
) -> Result<structs::SearchInfo, reqwest::Error> {
) -> Result<structs::SearchInfo, structs::Error> {
let uri = "https://nhentai.net/api/galleries/search";
let resp = client
.get(uri)
.query(&[("query", search_query)])
.send()
.await?;
Ok(serde_json::from_str(&resp.text().await?).unwrap())
Ok(serde_json::from_str(&resp.text().await?)?)
}

View File

@ -108,6 +108,36 @@ impl fmt::Display for GalleryInfoSuccess {
}
}
#[derive(Debug)]
pub enum Error {
Reqwest(reqwest::Error),
SerdeJSON(serde_json::Error),
}
impl From<reqwest::Error> for Error {
#[inline]
fn from(error: reqwest::Error) -> Error {
Error::Reqwest(error)
}
}
impl From<serde_json::Error> for Error {
#[inline]
fn from(error: serde_json::Error) -> Error {
Error::SerdeJSON(error)
}
}
impl fmt::Display for Error {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
let str = match self {
Error::Reqwest(err) => format!("reqwest error: {}", err),
Error::SerdeJSON(err) => format!("serde_json error: {}", err),
};
formatter.write_str(&str)
}
}
fn convert_to_i32<'de, D>(deserializer: D) -> Result<i32, D::Error>
where
D: Deserializer<'de>,