Skip to main content

gateio_rs/ureq/
error.rs

1use crate::http::error::{ClientError, HttpError as GateHttpError};
2use http::{Error as HttpError, uri::InvalidUri};
3use ureq::Error as UreqError;
4
5/// Communication error with the server.
6#[derive(Debug)]
7pub enum Error {
8    /// 4XX error from the server.
9    Client(ClientError),
10    /// 5XX error from the server.
11    Server(GateHttpError<String>),
12    /// The format of the API secret is invalid.
13    InvalidApiSecret,
14    /// Error serializing request payload to JSON
15    PayloadSerializationError,
16    /// Error parsing HTTP request or response
17    Parse(HttpError),
18    /// Error sending HTTP request
19    Send(UreqError),
20}
21
22impl From<InvalidUri> for Box<Error> {
23    fn from(err: InvalidUri) -> Box<Error> {
24        Box::new(Error::Parse(err.into()))
25    }
26}
27
28impl std::fmt::Display for Error {
29    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
30        match self {
31            Error::Client(e) => write!(f, "Client error: {:?}", e),
32            Error::Server(e) => write!(f, "Server error: {:?}", e),
33            Error::InvalidApiSecret => write!(f, "Invalid API secret"),
34            Error::PayloadSerializationError => write!(f, "Payload serialization error"),
35            Error::Parse(e) => write!(f, "Parse error: {}", e),
36            Error::Send(e) => write!(f, "Send error: {}", e),
37        }
38    }
39}
40
41impl std::error::Error for Error {}