longport_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: &'static str,
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
78/// Represents an HTTP error
79#[derive(Debug)]
80pub struct HttpError(pub reqwest::Error);
81
82impl From<reqwest::Error> for HttpError {
83    #[inline]
84    fn from(err: reqwest::Error) -> Self {
85        Self(err)
86    }
87}
88
89impl std::fmt::Display for HttpError {
90    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
91        if let Some(source) = self.0.source() {
92            write!(f, "{}: {}", self.0, source)
93        } else {
94            self.0.fmt(f)
95        }
96    }
97}
98
99impl std::error::Error for HttpError {}
100
101/// Http client result type
102pub type HttpClientResult<T, E = HttpClientError> = std::result::Result<T, E>;