forge_orchestration/
error.rs

1//! Error types for Forge
2//!
3//! ## Table of Contents
4//! - **ForgeError**: Main error enum covering all failure modes
5//! - **Result**: Type alias for `Result<T, ForgeError>`
6
7use thiserror::Error;
8
9/// Result type alias for Forge operations
10pub type Result<T> = std::result::Result<T, ForgeError>;
11
12/// Main error type for Forge operations
13#[derive(Error, Debug)]
14pub enum ForgeError {
15    /// Configuration error during builder setup
16    #[error("configuration error: {0}")]
17    Config(String),
18
19    /// Nomad API communication failure
20    #[error("nomad error: {0}")]
21    Nomad(String),
22
23    /// Storage backend failure (RocksDB or etcd)
24    #[error("storage error: {0}")]
25    Storage(String),
26
27    /// Networking failure (QUIC, HTTP, gRPC)
28    #[error("network error: {0}")]
29    Network(String),
30
31    /// Consensus/Raft failure
32    #[error("consensus error: {0}")]
33    Consensus(String),
34
35    /// MoE routing failure
36    #[error("routing error: {0}")]
37    Routing(String),
38
39    /// Job submission or management failure
40    #[error("job error: {0}")]
41    Job(String),
42
43    /// Autoscaling decision failure
44    #[error("autoscaler error: {0}")]
45    Autoscaler(String),
46
47    /// Metrics collection or export failure
48    #[error("metrics error: {0}")]
49    Metrics(String),
50
51    /// Runtime not initialized or already stopped
52    #[error("runtime error: {0}")]
53    Runtime(String),
54
55    /// Generic IO error
56    #[error("io error: {0}")]
57    Io(#[from] std::io::Error),
58
59    /// Serialization/deserialization error
60    #[error("serialization error: {0}")]
61    Serialization(#[from] serde_json::Error),
62
63    /// Internal error (should not occur in normal operation)
64    #[error("internal error: {0}")]
65    Internal(String),
66}
67
68impl ForgeError {
69    /// Create a configuration error
70    pub fn config(msg: impl Into<String>) -> Self {
71        Self::Config(msg.into())
72    }
73
74    /// Create a Nomad error
75    pub fn nomad(msg: impl Into<String>) -> Self {
76        Self::Nomad(msg.into())
77    }
78
79    /// Create a storage error
80    pub fn storage(msg: impl Into<String>) -> Self {
81        Self::Storage(msg.into())
82    }
83
84    /// Create a network error
85    pub fn network(msg: impl Into<String>) -> Self {
86        Self::Network(msg.into())
87    }
88
89    /// Create a job error
90    pub fn job(msg: impl Into<String>) -> Self {
91        Self::Job(msg.into())
92    }
93
94    /// Create a runtime error
95    pub fn runtime(msg: impl Into<String>) -> Self {
96        Self::Runtime(msg.into())
97    }
98
99    /// Create a metrics error
100    pub fn metrics(msg: impl Into<String>) -> Self {
101        Self::Metrics(msg.into())
102    }
103}
104
105impl From<reqwest::Error> for ForgeError {
106    fn from(err: reqwest::Error) -> Self {
107        Self::Network(err.to_string())
108    }
109}
110
111impl From<prometheus::Error> for ForgeError {
112    fn from(err: prometheus::Error) -> Self {
113        Self::Metrics(err.to_string())
114    }
115}