use std::time::Duration;
use thiserror::Error;
use crate::error_codes::{KrakenApiError as ParsedApiError, KrakenErrorCode, RecoveryStrategy};
#[derive(Error, Debug, Clone)]
#[non_exhaustive]
pub enum KrakenError {
#[error("Failed to connect to {url}: {reason}")]
ConnectionFailed {
url: String,
reason: String,
},
#[error("Connection timeout after {timeout:?} to {url}")]
ConnectionTimeout { url: String, timeout: Duration },
#[error("WebSocket error: {0}")]
WebSocket(String),
#[error("Invalid JSON: {message}")]
InvalidJson { message: String, raw: Option<String> },
#[error("Checksum mismatch for {symbol}: expected {expected}, computed {computed}")]
ChecksumMismatch {
symbol: String,
expected: u32,
computed: u32,
},
#[error("Unexpected message format: {0}")]
UnexpectedMessage(String),
#[error("Subscription rejected for {channel}: {reason}")]
SubscriptionRejected { channel: String, reason: String },
#[error("Symbol not found: {symbol}")]
SymbolNotFound { symbol: String },
#[error("Subscription timeout: no response within {timeout:?}")]
SubscriptionTimeout { timeout: Duration },
#[error("Authentication failed: {reason}")]
AuthenticationFailed { reason: String },
#[error("Token expired, please re-authenticate")]
TokenExpired,
#[error("Rate limited, retry after {retry_after:?}")]
RateLimited { retry_after: Duration },
#[error("Cloudflare connection limit exceeded, wait 10 minutes")]
CloudflareLimit,
#[error("Kraken API error: {message}")]
ApiError {
code: Option<KrakenErrorCode>,
message: String,
raw: String,
recovery: RecoveryStrategy,
},
#[error("Internal channel closed unexpectedly")]
ChannelClosed,
#[error("Shutdown in progress")]
ShuttingDown,
#[error("Invalid state: expected {expected}, got {actual}")]
InvalidState { expected: String, actual: String },
#[error("Configuration error: {0}")]
Configuration(String),
}
impl KrakenError {
pub fn is_retryable(&self) -> bool {
match self {
Self::ConnectionFailed { .. }
| Self::ConnectionTimeout { .. }
| Self::RateLimited { .. }
| Self::WebSocket(_)
| Self::ChecksumMismatch { .. } => true,
Self::ApiError { recovery, .. } => recovery.allows_retry(),
_ => false,
}
}
pub fn retry_after(&self) -> Option<Duration> {
match self {
Self::RateLimited { retry_after } => Some(*retry_after),
Self::CloudflareLimit => Some(Duration::from_secs(600)),
Self::ConnectionFailed { .. } => Some(Duration::from_millis(100)),
Self::ConnectionTimeout { .. } => Some(Duration::from_millis(500)),
Self::ApiError { recovery, .. } => recovery.initial_delay(),
_ => None,
}
}
pub fn requires_reconnect(&self) -> bool {
matches!(
self,
Self::WebSocket(_) | Self::ConnectionFailed { .. } | Self::ChannelClosed
)
}
pub fn requires_reauth(&self) -> bool {
match self {
Self::TokenExpired | Self::AuthenticationFailed { .. } => true,
Self::ApiError { recovery, .. } => {
matches!(recovery, RecoveryStrategy::Reauthenticate)
}
_ => false,
}
}
pub fn is_rate_limit(&self) -> bool {
match self {
Self::RateLimited { .. } | Self::CloudflareLimit => true,
Self::ApiError { code, .. } => {
code.map(|c| c.is_rate_limit()).unwrap_or(false)
}
_ => false,
}
}
pub fn recovery_strategy(&self) -> RecoveryStrategy {
match self {
Self::ApiError { recovery, .. } => recovery.clone(),
Self::RateLimited { .. } | Self::CloudflareLimit => {
RecoveryStrategy::rate_limit_backoff()
}
Self::ConnectionFailed { .. } | Self::ConnectionTimeout { .. } | Self::WebSocket(_) => {
RecoveryStrategy::Backoff {
initial_ms: 100,
max_ms: 30000,
multiplier: 2,
}
}
Self::ChecksumMismatch { .. } => RecoveryStrategy::RequestSnapshot,
Self::TokenExpired | Self::AuthenticationFailed { .. } => {
RecoveryStrategy::Reauthenticate
}
Self::ChannelClosed | Self::ShuttingDown => RecoveryStrategy::Fatal,
Self::InvalidState { .. } | Self::Configuration(_) => RecoveryStrategy::Fatal,
Self::InvalidJson { .. } | Self::UnexpectedMessage(_) => RecoveryStrategy::Skip,
Self::SubscriptionRejected { .. }
| Self::SymbolNotFound { .. }
| Self::SubscriptionTimeout { .. } => RecoveryStrategy::Skip,
}
}
pub fn checksum_mismatch(symbol: impl Into<String>, expected: u32, computed: u32) -> Self {
Self::ChecksumMismatch {
symbol: symbol.into(),
expected,
computed,
}
}
pub fn subscription_rejected(channel: impl Into<String>, reason: impl Into<String>) -> Self {
Self::SubscriptionRejected {
channel: channel.into(),
reason: reason.into(),
}
}
pub fn from_api_error(error: impl AsRef<str>) -> Self {
let parsed = ParsedApiError::parse(error.as_ref());
Self::ApiError {
code: parsed.code,
message: parsed.message.clone(),
raw: parsed.raw.clone(),
recovery: parsed.recovery_strategy(),
}
}
pub fn from_api_errors(errors: &[String]) -> Self {
if errors.is_empty() {
return Self::ApiError {
code: None,
message: "Unknown error".to_string(),
raw: String::new(),
recovery: RecoveryStrategy::Manual,
};
}
Self::from_api_error(&errors[0])
}
pub fn error_code(&self) -> Option<KrakenErrorCode> {
match self {
Self::ApiError { code, .. } => *code,
_ => None,
}
}
}
pub type KrakenResult<T> = Result<T, KrakenError>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_retryable() {
let err = KrakenError::RateLimited {
retry_after: Duration::from_secs(5),
};
assert!(err.is_retryable());
assert_eq!(err.retry_after(), Some(Duration::from_secs(5)));
let err = KrakenError::AuthenticationFailed {
reason: "bad token".into(),
};
assert!(!err.is_retryable());
assert_eq!(err.retry_after(), None);
}
#[test]
fn test_error_requires_reconnect() {
let err = KrakenError::WebSocket("connection reset".into());
assert!(err.requires_reconnect());
let err = KrakenError::ChecksumMismatch {
symbol: "BTC/USD".into(),
expected: 123,
computed: 456,
};
assert!(!err.requires_reconnect());
}
#[test]
fn test_from_api_error() {
let err = KrakenError::from_api_error("EAPI:Rate limit exceeded");
assert!(err.is_rate_limit());
assert!(err.is_retryable());
assert_eq!(err.error_code(), Some(KrakenErrorCode::RateLimitExceeded));
}
#[test]
fn test_from_api_error_auth() {
let err = KrakenError::from_api_error("EAPI:Invalid key");
assert!(err.requires_reauth());
assert_eq!(err.error_code(), Some(KrakenErrorCode::InvalidKey));
}
#[test]
fn test_from_api_error_insufficient_funds() {
let err = KrakenError::from_api_error("EOrder:Insufficient funds");
assert!(!err.is_retryable());
assert!(matches!(
err.recovery_strategy(),
RecoveryStrategy::UserAction { .. }
));
}
#[test]
fn test_from_api_errors() {
let errors = vec![
"EOrder:Insufficient funds".to_string(),
"EOrder:Order minimum not met".to_string(),
];
let err = KrakenError::from_api_errors(&errors);
assert_eq!(err.error_code(), Some(KrakenErrorCode::InsufficientFunds));
}
#[test]
fn test_recovery_strategy() {
let err = KrakenError::ChecksumMismatch {
symbol: "BTC/USD".into(),
expected: 123,
computed: 456,
};
assert!(matches!(
err.recovery_strategy(),
RecoveryStrategy::RequestSnapshot
));
let err = KrakenError::TokenExpired;
assert!(matches!(
err.recovery_strategy(),
RecoveryStrategy::Reauthenticate
));
}
}