dehashed_rs/
error.rs

1use std::fmt::{Display, Formatter};
2use std::net::AddrParseError;
3
4use reqwest::header::InvalidHeaderValue;
5
6/// The common error type of this crate
7#[derive(Debug)]
8pub enum DehashedError {
9    /// Error that are caused by reqwest
10    ReqwestError(reqwest::Error),
11    /// Invalid API credentials
12    Unauthorized,
13    /// Query is missing or invalid
14    InvalidQuery,
15    /// The used account got rate limited
16    RateLimited,
17    /// An unknown error occurred
18    Unknown(String),
19    /// An error occurred while parsing an int field
20    ParseIntError(std::num::ParseIntError),
21    /// An error occurred while parsing an ip addr field
22    ParseAddrError(AddrParseError),
23    /// Invalid header value
24    InvalidApiKey(InvalidHeaderValue),
25}
26
27impl Display for DehashedError {
28    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
29        match self {
30            DehashedError::ReqwestError(err) => write!(f, "Reqwest error occurred: {err}"),
31            DehashedError::Unauthorized => write!(f, "Invalid API credentials"),
32            DehashedError::InvalidQuery => write!(f, "The provided query is missing or invalid"),
33            DehashedError::RateLimited => write!(f, "The account got rate limited"),
34            DehashedError::Unknown(err) => write!(f, "An unknown error occurred: {err}"),
35            DehashedError::ParseIntError(err) => {
36                write!(f, "An error occurred while parsing a response: {err}")
37            }
38            DehashedError::ParseAddrError(err) => write!(f, "Error while parsing ip addr: {err}"),
39            DehashedError::InvalidApiKey(err) => {
40                write!(f, "Invalid API key, could not put in header: {err}")
41            }
42        }
43    }
44}
45
46impl std::error::Error for DehashedError {}
47
48impl From<reqwest::Error> for DehashedError {
49    fn from(value: reqwest::Error) -> Self {
50        Self::ReqwestError(value)
51    }
52}
53
54impl From<std::num::ParseIntError> for DehashedError {
55    fn from(value: std::num::ParseIntError) -> Self {
56        Self::ParseIntError(value)
57    }
58}
59
60impl From<AddrParseError> for DehashedError {
61    fn from(value: AddrParseError) -> Self {
62        Self::ParseAddrError(value)
63    }
64}
65
66impl From<InvalidHeaderValue> for DehashedError {
67    fn from(value: InvalidHeaderValue) -> Self {
68        Self::InvalidApiKey(value)
69    }
70}