use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BitgetApiError {
pub code: String,
pub msg: String,
#[serde(default)]
pub request_id: String,
}
impl fmt::Display for BitgetApiError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"Bitget API 错误 - 代码: {}, 消息: {}",
self.code, self.msg
)
}
}
impl std::error::Error for BitgetApiError {}
pub fn parse_error_response(response_text: &str) -> Option<BitgetApiError> {
match serde_json::from_str::<serde_json::Value>(response_text) {
Ok(value) => {
if let (Some(code), Some(msg)) = (value.get("code"), value.get("msg")) {
if code.as_str().map_or(false, |c| c != "00000") {
let request_id = value
.get("requestId")
.and_then(|id| id.as_str())
.unwrap_or("")
.to_string();
return Some(BitgetApiError {
code: code.as_str().unwrap_or("").to_string(),
msg: msg.as_str().unwrap_or("").to_string(),
request_id,
});
}
}
None
}
Err(_) => None,
}
}