Skip to main content

longbridge_httpcli/
error.rs

1use std::error::Error;
2
3use reqwest::StatusCode;
4
5use crate::qs::QsError;
6
7/// Http client error type
8#[derive(Debug, thiserror::Error)]
9pub enum HttpClientError {
10    /// Invalid request method
11    #[error("invalid request method")]
12    InvalidRequestMethod,
13
14    /// Invalid api key
15    #[error("invalid api key")]
16    InvalidApiKey,
17
18    /// Invalid access token
19    #[error("invalid access token")]
20    InvalidAccessToken,
21
22    /// Missing environment variable
23    #[error("missing environment variable: {name}")]
24    MissingEnvVar {
25        /// Variable name
26        name: String,
27    },
28
29    /// Unexpected response
30    #[error("unexpected response")]
31    UnexpectedResponse,
32
33    /// Request timeout
34    #[error("request timeout")]
35    RequestTimeout,
36
37    /// OpenAPI error
38    #[error("openapi error: code={code}: {message}")]
39    OpenApi {
40        /// Error code
41        code: i32,
42        /// Error message
43        message: String,
44        /// Trace id
45        trace_id: String,
46    },
47
48    /// Deserialize response body
49    #[error("deserialize response body error: {0}")]
50    DeserializeResponseBody(String),
51
52    /// Serialize request body
53    #[error("serialize request body error: {0}")]
54    SerializeRequestBody(String),
55
56    /// Serialize query string error
57    #[error("serialize query string error: {0}")]
58    SerializeQueryString(#[from] QsError),
59
60    /// Bad status
61    #[error("status error: {0}")]
62    BadStatus(StatusCode),
63
64    /// Http error
65    #[error(transparent)]
66    Http(#[from] HttpError),
67
68    /// Connection limit exceeded
69    #[error("connections limitation is hit, limit = {limit}, online = {online}")]
70    ConnectionLimitExceeded {
71        /// The limit of connections
72        limit: i32,
73        /// The number of online connections
74        online: i32,
75    },
76
77    /// OAuth error
78    #[error("oauth error: {0}")]
79    OAuth(String),
80}
81
82/// Represents an HTTP error
83#[derive(Debug)]
84pub struct HttpError(pub reqwest::Error);
85
86impl From<reqwest::Error> for HttpError {
87    #[inline]
88    fn from(err: reqwest::Error) -> Self {
89        Self(err)
90    }
91}
92
93impl std::fmt::Display for HttpError {
94    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
95        if let Some(source) = self.0.source() {
96            write!(f, "{}: {}", self.0, source)
97        } else {
98            self.0.fmt(f)
99        }
100    }
101}
102
103impl std::error::Error for HttpError {}
104
105/// Http client result type
106pub type HttpClientResult<T, E = HttpClientError> = std::result::Result<T, E>;