Skip to main content

nautilus_coinapi/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//! CoinAPI protocol and request failures.
17
18use nautilus_network::{http::HttpClientError, retry::RetryError};
19
20/// A failure at the CoinAPI REST boundary.
21#[derive(Debug, thiserror::Error)]
22pub enum Error {
23    #[error(transparent)]
24    Transport(#[from] HttpClientError),
25    #[error("CoinAPI HTTP status {status}")]
26    Http {
27        status: u16,
28        retry_after_secs: Option<u64>,
29    },
30    #[error("CoinAPI HTTP status {status}: {message}")]
31    Venue {
32        status: u16,
33        message: String,
34        retry_after_secs: Option<u64>,
35    },
36    #[error("CoinAPI response parsing failed: {0}")]
37    Parse(#[from] serde_json::Error),
38    #[error("Invalid CoinAPI request: {0}")]
39    Validation(String),
40    #[error("Unexpected CoinAPI response: {0}")]
41    Response(String),
42    #[error("CoinAPI credit budget exhausted")]
43    CreditBudget,
44    #[error("CoinAPI retry control: {0}")]
45    Retry(RetryError),
46}
47
48impl Error {
49    /// Returns whether a read request may retry this failure.
50    #[must_use]
51    pub fn is_retryable(&self) -> bool {
52        match self {
53            Self::Transport(
54                HttpClientError::TransportError(_) | HttpClientError::TimeoutError(_),
55            )
56            | Self::Retry(RetryError::OperationTimeout { .. }) => true,
57            Self::Http { status, .. } | Self::Venue { status, .. } => {
58                *status == 429 || matches!(status, 500 | 502 | 503 | 504)
59            }
60            _ => false,
61        }
62    }
63
64    pub(super) fn retry_delay(&self) -> Option<std::time::Duration> {
65        match self {
66            Self::Http {
67                retry_after_secs, ..
68            }
69            | Self::Venue {
70                retry_after_secs, ..
71            } => retry_after_secs.map(std::time::Duration::from_secs),
72            _ => None,
73        }
74    }
75}