Skip to main content

nautilus_tardis/common/
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//! Adapter-level error types aggregating HTTP and Machine Server errors.
17
18/// Adapter-level error aggregating HTTP and WebSocket errors.
19#[derive(Debug, thiserror::Error)]
20pub enum TardisError {
21    /// An HTTP API error.
22    #[error("HTTP error: {0}")]
23    Http(#[from] crate::http::error::Error),
24
25    /// A Machine Server WebSocket error.
26    #[error("Machine error: {0}")]
27    Machine(#[from] crate::machine::Error),
28}
29
30impl TardisError {
31    /// Returns `true` if the error is likely transient and the operation can be
32    /// retried.
33    #[must_use]
34    pub fn is_retryable(&self) -> bool {
35        match self {
36            Self::Http(crate::http::error::Error::ApiError { status, .. }) => {
37                *status == 429 || *status >= 500
38            }
39            Self::Http(crate::http::error::Error::Request(_)) => true,
40            Self::Machine(crate::machine::Error::ConnectFailed(_)) => true,
41            Self::Machine(crate::machine::Error::ConnectionClosed { reason }) => {
42                !crate::machine::is_unsupported_streaming_error(reason)
43            }
44            _ => false,
45        }
46    }
47}
48
49#[cfg(test)]
50mod tests {
51    use rstest::rstest;
52
53    use super::*;
54    use crate::{common::enums::TardisExchange, machine::Error};
55
56    #[rstest]
57    #[case(
58        "Error: Real-time streaming is not supported for exchange bitmex",
59        false
60    )]
61    #[case("Real-time streaming is not supported for exchange coinflex", false)]
62    #[case("Too many subsequent errors when connecting to deribit WS API", true)]
63    fn test_machine_close_retryable(#[case] reason: &str, #[case] expected: bool) {
64        let error = TardisError::Machine(Error::ConnectionClosed {
65            reason: reason.to_string(),
66        });
67
68        assert_eq!(error.is_retryable(), expected);
69    }
70
71    #[rstest]
72    fn test_unsupported_streaming_exchange_not_retryable() {
73        let error =
74            TardisError::Machine(Error::UnsupportedStreamingExchange(TardisExchange::Bitmex));
75
76        assert!(!error.is_retryable());
77    }
78}