Skip to main content

ccxt_core/error/
details.rs

1//! Error detail structures for various error types.
2
3use serde_json::Value;
4
5#[cfg(feature = "backtrace")]
6use std::backtrace::Backtrace;
7
8/// Details for exchange-specific errors.
9///
10/// Extracted to a separate struct and boxed to keep Error enum size small.
11///
12/// Note: `#[non_exhaustive]` allows adding fields in future versions without breaking changes.
13///
14/// # Example
15///
16/// ```rust
17/// use ccxt_core::error::ExchangeErrorDetails;
18///
19/// let details = ExchangeErrorDetails::new("400", "Bad Request");
20/// assert_eq!(details.code, "400");
21/// ```
22#[derive(Debug)]
23#[non_exhaustive]
24pub struct ExchangeErrorDetails {
25    /// Error code as String to support all exchange formats (numeric, alphanumeric).
26    pub code: String,
27    /// Descriptive message from the exchange.
28    pub message: String,
29    /// Optional raw response data for debugging.
30    pub data: Option<Value>,
31    /// Backtrace captured at error creation (feature-gated).
32    #[cfg(feature = "backtrace")]
33    pub backtrace: Backtrace,
34}
35
36impl ExchangeErrorDetails {
37    /// Creates a new `ExchangeErrorDetails` with the given code and message.
38    pub fn new(code: impl Into<String>, message: impl Into<String>) -> Self {
39        Self {
40            code: code.into(),
41            message: message.into(),
42            data: None,
43            #[cfg(feature = "backtrace")]
44            backtrace: Backtrace::capture(),
45        }
46    }
47
48    /// Creates a new `ExchangeErrorDetails` with raw response data.
49    pub fn with_data(code: impl Into<String>, message: impl Into<String>, data: Value) -> Self {
50        Self {
51            code: code.into(),
52            message: message.into(),
53            data: Some(data),
54            #[cfg(feature = "backtrace")]
55            backtrace: Backtrace::capture(),
56        }
57    }
58}
59
60impl std::fmt::Display for ExchangeErrorDetails {
61    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
62        write!(f, "{} (code: {})", self.message, self.code)
63    }
64}