Skip to content

Commit af5b097

Browse files
authored
feat(binance-api): handle API error responses gracefully (#3424)
Introduce `BinanceApiResponse` enum to handle both success and error responses from the Binance API.
1 parent 8798344 commit af5b097

File tree

1 file changed

+35
-8
lines changed
  • tee-worker/omni-executor/binance-api/src

1 file changed

+35
-8
lines changed

tee-worker/omni-executor/binance-api/src/lib.rs

+35-8
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ pub struct BinanceApi {
2929
api_secret: String,
3030
}
3131

32+
#[derive(serde::Deserialize)]
33+
#[serde(untagged)]
34+
pub enum BinanceApiResponse<T> {
35+
Success(T),
36+
Error { code: i32, msg: String },
37+
}
38+
3239
impl BinanceApi {
3340
pub fn new(api_key: String, api_secret: String, base_url: Option<String>) -> BinanceApi {
3441
let base_url = match base_url {
@@ -85,10 +92,20 @@ impl BinanceApi {
8592

8693
debug!("Binance-api make_public_get_request response: {:?}", response);
8794

88-
response.json::<T>().await.map_err(|e| {
89-
error!("Error parsing response: {}", e);
90-
Error::ParseResponseFailed
91-
})
95+
response
96+
.json::<BinanceApiResponse<T>>()
97+
.await
98+
.map_err(|e| {
99+
error!("Error parsing response: {}", e);
100+
Error::ParseResponseFailed
101+
})
102+
.and_then(|res| match res {
103+
BinanceApiResponse::Success(data) => Ok(data),
104+
BinanceApiResponse::Error { code, msg } => {
105+
error!("Binance API Error: {} - {}", code, msg);
106+
Err(Error::RequestFailed)
107+
},
108+
})
92109
}
93110

94111
/// Helper for making signed API requests
@@ -155,10 +172,20 @@ impl BinanceApi {
155172

156173
debug!("Binance-api make_signed_request response: {:?}", response);
157174

158-
response.json::<T>().await.map_err(|e| {
159-
error!("Error parsing response: {}", e);
160-
Error::ParseResponseFailed
161-
})
175+
response
176+
.json::<BinanceApiResponse<T>>()
177+
.await
178+
.map_err(|e| {
179+
error!("Error parsing response: {}", e);
180+
Error::ParseResponseFailed
181+
})
182+
.and_then(|res| match res {
183+
BinanceApiResponse::Success(data) => Ok(data),
184+
BinanceApiResponse::Error { code, msg } => {
185+
error!("Binance API Error: {} - {}", code, msg);
186+
Err(Error::RequestFailed)
187+
},
188+
})
162189
}
163190
}
164191

0 commit comments

Comments
 (0)