use serde::Serialize;
use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub enum ApiErrorCode {
InternalError = 1000,
InvalidRequest = 1001,
ParseError = 1002,
RateLimitExceeded = 1003,
GeoRestricted = 1004,
MarketNotFound = 2000,
MarketPaused = 2001,
OrderNotFound = 3000,
InvalidSignature = 4000,
WhitelistNotConfigured = 4003,
TradeNotFound = 5000,
InvalidTradeCount = 5001,
AlreadySubscribed = 6000,
TooManySubscriptions = 6001,
SubscriptionError = 6002,
InvalidAmount = 7000,
InvalidTimeRange = 7001,
InvalidPagination = 7002,
NoActionsProvided = 7003,
TooManyActions = 7004,
BlockNotFound = 8000,
EventsNotFound = 8001,
}
#[derive(Debug)]
pub struct ApiError {
pub code: ApiErrorCode,
pub message: String,
}
impl ApiError {
pub fn new(code: ApiErrorCode, message: impl Into<String>) -> Self {
Self {
code,
message: message.into(),
}
}
pub fn error_code_number(&self) -> u32 {
self.code as u32
}
}
impl fmt::Display for ApiError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.message)
}
}
impl std::error::Error for ApiError {}
pub fn extract_api_error(err: &anyhow::Error) -> (u32, String) {
match err.downcast_ref::<ApiError>() {
Some(api_err) => (api_err.error_code_number(), api_err.message.clone()),
None => (ApiErrorCode::InternalError as u32, err.to_string()),
}
}