Skip to main content

rustrade_core/
error.rs

1//! Unified error type for the rustrade framework.
2//!
3//! Downstream crates should either use this type directly or wrap it in their
4//! own error enum via `#[from]`. The variants are intentionally coarse — this
5//! is a framework-level type, not a domain-specific one.
6
7use thiserror::Error;
8
9/// Result alias used throughout rustrade.
10pub type Result<T> = std::result::Result<T, Error>;
11
12/// Framework-level errors.
13///
14/// Exchange adapters, brain implementations, and risk checks wrap their
15/// domain-specific errors in one of these variants.
16#[derive(Debug, Error)]
17pub enum Error {
18    /// The exchange rejected the request, or the connection to it failed.
19    #[error("exchange error: {0}")]
20    Exchange(String),
21
22    /// The brain returned an error while processing a market event.
23    #[error("brain error: {0}")]
24    Brain(String),
25
26    /// A risk check blocked an action.
27    #[error("risk check blocked: {0}")]
28    Risk(String),
29
30    /// Configuration is invalid or incomplete.
31    #[error("configuration error: {0}")]
32    Config(String),
33
34    /// Serialization or deserialization failed.
35    #[error("serde error: {0}")]
36    Serde(#[from] serde_json::Error),
37
38    /// A state-store persistence failure (backend I/O, corrupt snapshot).
39    #[error("state store error: {0}")]
40    Storage(String),
41
42    /// An otherwise-uncategorised framework error.
43    #[error("internal error: {0}")]
44    Internal(String),
45}
46
47impl Error {
48    /// Convenience constructor for `Exchange` variant.
49    pub fn exchange(msg: impl Into<String>) -> Self {
50        Self::Exchange(msg.into())
51    }
52
53    /// Convenience constructor for `Brain` variant.
54    pub fn brain(msg: impl Into<String>) -> Self {
55        Self::Brain(msg.into())
56    }
57
58    /// Convenience constructor for `Risk` variant.
59    pub fn risk(msg: impl Into<String>) -> Self {
60        Self::Risk(msg.into())
61    }
62
63    /// Convenience constructor for `Config` variant.
64    pub fn config(msg: impl Into<String>) -> Self {
65        Self::Config(msg.into())
66    }
67
68    /// Convenience constructor for `Internal` variant.
69    pub fn internal(msg: impl Into<String>) -> Self {
70        Self::Internal(msg.into())
71    }
72
73    /// Convenience constructor for `Storage` variant.
74    pub fn storage(msg: impl Into<String>) -> Self {
75        Self::Storage(msg.into())
76    }
77}