ergoreq/
error.rs

1use chrono::OutOfRangeError;
2
3#[derive(Debug)]
4pub enum Error {
5    Reqwest(reqwest::Error),
6    TooManyRedirect(url::Url, u64),
7    RedirectLocationInvalid,
8    RedirectLocationEmpty,
9    Http(http::Error),
10    Custom(Box<dyn std::error::Error + Send + Sync + 'static>),
11    InvalidRedirectUrl(String),
12    Internal(Box<dyn std::error::Error + Send + Sync + 'static>),
13}
14
15pub type Result<T> = core::result::Result<T, Error>;
16
17impl std::fmt::Display for Error {
18    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19        match self {
20            Error::Reqwest(inner) => write!(f, "Reqwest error: {:?}", inner),
21            Error::Custom(inner) => write!(f, "Custom error: {:?}", inner),
22            Error::Http(inner) => write!(f, "Build http error: {:?}", inner),
23            Error::TooManyRedirect(origin_url, redirect_count) => write!(
24                f,
25                "Too many redirect for request to '{origin_url}': {redirect_count} time(s)."
26            ),
27            Error::InvalidRedirectUrl(url) => write!(f, "The redirect url is invalid: {url}"),
28            Error::Internal(e) => write!(f, "Ergo internal error: {:?}", e),
29            Error::RedirectLocationInvalid => {
30                write!(f, "The redirect location is invalid")
31            }
32            Error::RedirectLocationEmpty => write!(f, "The redirect location is empty"),
33        }
34    }
35}
36
37impl std::error::Error for Error {}
38
39impl From<anyhow::Error> for Error {
40    fn from(value: anyhow::Error) -> Self {
41        Self::Custom(value.into())
42    }
43}
44
45impl From<reqwest::Error> for Error {
46    fn from(value: reqwest::Error) -> Self {
47        Self::Reqwest(value)
48    }
49}
50
51impl From<http::Error> for Error {
52    fn from(value: http::Error) -> Self {
53        Self::Http(value)
54    }
55}
56
57impl From<chrono::OutOfRangeError> for Error {
58    fn from(value: OutOfRangeError) -> Self {
59        Self::Internal(Box::new(value))
60    }
61}