Skip to main content

nautilus_betfair/stream/
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//! Betfair stream client error types.
17
18/// Represents stream client errors for the Betfair adapter.
19#[derive(Debug, Clone, thiserror::Error)]
20pub enum BetfairStreamError {
21    /// Failed to establish a connection.
22    #[error("Connection failed: {0}")]
23    ConnectionFailed(String),
24    /// Stream authentication failed.
25    #[error("Authentication failed: {0}")]
26    AuthenticationFailed(String),
27    /// Stream protocol error (unexpected message format).
28    #[error("Protocol error: {0}")]
29    ProtocolError(String),
30    /// JSON serialization/deserialization error.
31    #[error("JSON error: {0}")]
32    JsonError(String),
33    /// Connection or read timeout.
34    #[error("Timeout: {0}")]
35    Timeout(String),
36    /// Connection was lost.
37    #[error("Disconnected: {0}")]
38    Disconnected(String),
39}
40
41impl From<serde_json::Error> for BetfairStreamError {
42    fn from(error: serde_json::Error) -> Self {
43        Self::JsonError(error.to_string())
44    }
45}
46
47#[cfg(test)]
48mod tests {
49    use rstest::rstest;
50
51    use super::*;
52
53    #[rstest]
54    #[case(
55        BetfairStreamError::ConnectionFailed("refused".to_string()),
56        "Connection failed: refused"
57    )]
58    #[case(
59        BetfairStreamError::AuthenticationFailed("invalid token".to_string()),
60        "Authentication failed: invalid token"
61    )]
62    #[case(
63        BetfairStreamError::ProtocolError("bad frame".to_string()),
64        "Protocol error: bad frame"
65    )]
66    #[case(
67        BetfairStreamError::JsonError("parse error".to_string()),
68        "JSON error: parse error"
69    )]
70    #[case(
71        BetfairStreamError::Timeout("read".to_string()),
72        "Timeout: read"
73    )]
74    #[case(
75        BetfairStreamError::Disconnected("reset".to_string()),
76        "Disconnected: reset"
77    )]
78    fn test_display(#[case] error: BetfairStreamError, #[case] expected: &str) {
79        assert_eq!(error.to_string(), expected);
80    }
81
82    #[rstest]
83    fn test_from_serde_error() {
84        let json_err = serde_json::from_str::<String>("bad").unwrap_err();
85        let err: BetfairStreamError = json_err.into();
86        assert!(matches!(err, BetfairStreamError::JsonError(_)));
87    }
88}