Skip to main content

ccxt_core/error/
network.rs

1//! Network-related error types.
2
3use std::error::Error as StdError;
4use thiserror::Error;
5
6/// Encapsulated network errors hiding implementation details.
7///
8/// This type wraps all network-related errors without exposing third-party
9/// library types (like `reqwest::Error`) in the public API. This ensures
10/// API stability even when underlying HTTP libraries change.
11///
12/// # Retryable Errors
13///
14/// The following variants are considered retryable:
15/// - [`NetworkError::Timeout`] - Request timed out, may succeed on retry
16/// - [`NetworkError::ConnectionFailed`] - Connection failed, may be transient
17///
18/// # Example
19///
20/// ```rust
21/// use ccxt_core::error::{Error, NetworkError};
22///
23/// fn handle_network_error(err: NetworkError) {
24///     match &err {
25///         NetworkError::RequestFailed { status, message } => {
26///             println!("HTTP {}: {}", status, message);
27///         }
28///         NetworkError::Timeout => {
29///             println!("Request timed out, consider retrying");
30///         }
31///         NetworkError::ConnectionFailed(msg) => {
32///             println!("Connection failed: {}", msg);
33///         }
34///         _ => println!("Network error: {}", err),
35///     }
36/// }
37/// ```
38#[derive(Error, Debug)]
39#[non_exhaustive]
40pub enum NetworkError {
41    /// Request failed with HTTP status code.
42    #[error("Request failed with status {status}: {message}")]
43    RequestFailed {
44        /// HTTP status code
45        status: u16,
46        /// Error message
47        message: String,
48    },
49
50    /// Request timed out.
51    #[error("Request timeout")]
52    Timeout,
53
54    /// Connection failed.
55    #[error("Connection failed: {0}")]
56    ConnectionFailed(String),
57
58    /// DNS resolution failed.
59    #[error("DNS resolution failed: {0}")]
60    DnsResolution(String),
61
62    /// SSL/TLS error.
63    #[error("SSL/TLS error: {0}")]
64    Ssl(String),
65
66    /// Opaque transport error for underlying issues.
67    /// Uses `Box<dyn StdError>` to hide implementation details while preserving the source.
68    #[error("Transport error")]
69    Transport(#[source] Box<dyn StdError + Send + Sync + 'static>),
70}