nautilus_kraken/http/
error.rs1use thiserror::Error;
19
20#[derive(Debug, Clone, Error)]
21pub enum KrakenHttpError {
22 #[error("Request not started: {0}")]
23 RequestNotStarted(String),
24
25 #[error("Network error: {0}")]
26 NetworkError(String),
27
28 #[error("API error: {}", format_api_errors(.0))]
29 ApiError(Vec<String>),
30
31 #[error("Parse error: {0}")]
32 ParseError(String),
33
34 #[error("Authentication error: {0}")]
35 AuthenticationError(String),
36
37 #[error("Missing credentials")]
38 MissingCredentials,
39}
40
41#[derive(Debug, Error)]
42pub(crate) enum KrakenSubmitOrderError {
43 #[error("Order rejected: {reason}")]
44 Rejected { reason: String },
45
46 #[error("No send status in successful response")]
47 MissingStatus,
48
49 #[error("Unknown send status: {status}")]
50 UnknownStatus { status: String },
51
52 #[error("No order ID in submit response: {detail}")]
53 MissingOrderId { detail: String },
54
55 #[error("{detail}")]
56 InvalidOrderId { detail: String },
57
58 #[error("Order lookup failed after submission: {source}")]
59 PostSubmitLookup {
60 #[source]
61 source: anyhow::Error,
62 },
63}
64
65#[derive(Debug, Error)]
66pub(crate) enum KrakenModifyOrderError {
67 #[error("Order modification rejected: {reason}")]
68 Rejected { reason: String },
69
70 #[error("Unknown edit status: {status}")]
71 UnknownStatus { status: String },
72
73 #[error("No order ID in edit response")]
74 MissingOrderId,
75
76 #[error("{detail}")]
77 InvalidOrderId { detail: String },
78}
79
80#[derive(Debug, Error)]
81pub(crate) enum KrakenBatchOrderError {
82 #[error("Order validation failed: {reason}")]
83 Validation { reason: String },
84
85 #[error("Order not sent after an earlier chunk failed")]
86 NotAttempted,
87
88 #[error("Batch response item count {actual} did not match request count {expected}")]
89 ResponseCount { expected: usize, actual: usize },
90
91 #[error("Missing batch response for {key}")]
92 MissingResponse { key: String },
93
94 #[error("Duplicate batch responses for {key}")]
95 DuplicateResponse { key: String },
96}
97
98fn format_api_errors(errors: &[String]) -> String {
100 if errors.is_empty() {
101 "unknown error (empty error list)".to_string()
102 } else {
103 errors.join(", ")
104 }
105}
106
107impl From<anyhow::Error> for KrakenHttpError {
108 fn from(err: anyhow::Error) -> Self {
109 Self::NetworkError(err.to_string())
110 }
111}
112
113pub fn kraken_http_should_retry(error: &KrakenHttpError) -> bool {
115 match error {
116 KrakenHttpError::NetworkError(_) => true,
117 KrakenHttpError::ApiError(errors) => errors.iter().any(|e| e.contains("Rate limit")),
118 KrakenHttpError::RequestNotStarted(_)
119 | KrakenHttpError::ParseError(_)
120 | KrakenHttpError::AuthenticationError(_)
121 | KrakenHttpError::MissingCredentials => false,
122 }
123}