Skip to main content

nautilus_kraken/http/
error.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16//! Error types for Kraken HTTP client operations.
17
18use 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
98/// Formats API error messages, handling empty error arrays.
99fn 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
113/// Returns `true` if a request producing this error should be retried.
114pub 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}