longbridge_httpcli/
error.rs1use std::error::Error;
2
3use reqwest::StatusCode;
4
5use crate::qs::QsError;
6
7#[derive(Debug, thiserror::Error)]
9pub enum HttpClientError {
10 #[error("invalid request method")]
12 InvalidRequestMethod,
13
14 #[error("invalid api key")]
16 InvalidApiKey,
17
18 #[error("invalid access token")]
20 InvalidAccessToken,
21
22 #[error("missing environment variable: {name}")]
24 MissingEnvVar {
25 name: String,
27 },
28
29 #[error("unexpected response")]
31 UnexpectedResponse,
32
33 #[error("request timeout")]
35 RequestTimeout,
36
37 #[error("openapi error: code={code}: {message}")]
39 OpenApi {
40 code: i32,
42 message: String,
44 trace_id: String,
46 },
47
48 #[error("deserialize response body error: {0}")]
50 DeserializeResponseBody(String),
51
52 #[error("serialize request body error: {0}")]
54 SerializeRequestBody(String),
55
56 #[error("serialize query string error: {0}")]
58 SerializeQueryString(#[from] QsError),
59
60 #[error("status error: {0}")]
62 BadStatus(StatusCode),
63
64 #[error(transparent)]
66 Http(#[from] HttpError),
67
68 #[error("connections limitation is hit, limit = {limit}, online = {online}")]
70 ConnectionLimitExceeded {
71 limit: i32,
73 online: i32,
75 },
76
77 #[error("oauth error: {0}")]
79 OAuth(String),
80}
81
82#[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
105pub type HttpClientResult<T, E = HttpClientError> = std::result::Result<T, E>;