Skip to main content

flare_core_runtime/
error.rs

1//! 统一错误类型定义
2//!
3//! 使用 thiserror 定义所有错误类型,确保错误信息清晰、可追踪
4
5use std::time::Duration;
6use thiserror::Error;
7
8/// 运行时错误
9#[derive(Debug, Error)]
10pub enum RuntimeError {
11    #[error("Task '{name}' failed: {source}")]
12    TaskFailed {
13        name: String,
14        #[source]
15        source: Box<dyn std::error::Error + Send + Sync>,
16    },
17
18    #[error("Circular dependency detected: {tasks:?}")]
19    CircularDependency { tasks: Vec<String> },
20
21    #[error("Task '{task}' depends on '{dependency}', but '{dependency}' is not registered")]
22    MissingDependency { task: String, dependency: String },
23
24    #[error("Task '{name}' startup timeout after {timeout:?}")]
25    StartupTimeout { name: String, timeout: Duration },
26
27    #[error("Task '{name}' shutdown timeout after {timeout:?}")]
28    ShutdownTimeout { name: String, timeout: Duration },
29
30    #[error("Health check failed for '{name}': {source}")]
31    HealthCheckFailed {
32        name: String,
33        #[source]
34        source: HealthError,
35    },
36
37    #[error("Service registration failed: {0}")]
38    RegistrationFailed(#[from] RegistryError),
39
40    #[error("Plugin '{name}' initialization failed: {source}")]
41    PluginInitFailed {
42        name: String,
43        #[source]
44        source: PluginError,
45    },
46
47    #[error("Invalid configuration: {0}")]
48    InvalidConfig(String),
49
50    #[error("Runtime is already running")]
51    AlreadyRunning,
52
53    #[error("Runtime is not running")]
54    NotRunning,
55}
56
57/// 服务注册错误
58#[derive(Debug, Error)]
59pub enum RegistryError {
60    #[error("Failed to connect to registry server: {0}")]
61    ConnectionFailed(String),
62
63    #[error("Failed to register service '{service_id}': {reason}")]
64    RegistrationFailed { service_id: String, reason: String },
65
66    #[error("Failed to deregister service '{service_id}': {reason}")]
67    DeregistrationFailed { service_id: String, reason: String },
68
69    #[error("Failed to send heartbeat for service '{service_id}': {reason}")]
70    HeartbeatFailed { service_id: String, reason: String },
71
72    #[error("Service '{service_id}' not found")]
73    ServiceNotFound { service_id: String },
74
75    #[error("Registry timeout after {timeout:?}")]
76    Timeout { timeout: Duration },
77}
78
79/// 健康检查错误
80#[derive(Debug, Error)]
81pub enum HealthError {
82    #[error("Health check '{name}' failed: {reason}")]
83    CheckFailed { name: String, reason: String },
84
85    #[error("Health check '{name}' timeout after {timeout:?}")]
86    Timeout { name: String, timeout: Duration },
87
88    #[error("Health check '{name}' is unhealthy: {details}")]
89    Unhealthy { name: String, details: String },
90
91    #[error("Consecutive failures exceeded threshold: {failures}/{threshold}")]
92    ExceededThreshold { failures: u32, threshold: u32 },
93}
94
95/// 插件错误
96#[derive(Debug, Error)]
97pub enum PluginError {
98    #[error("Plugin '{name}' initialization failed: {reason}")]
99    InitFailed { name: String, reason: String },
100
101    #[error("Plugin '{name}' execution failed: {reason}")]
102    ExecutionFailed { name: String, reason: String },
103
104    #[error("Plugin '{name}' not found")]
105    NotFound { name: String },
106
107    #[error("Plugin hook '{hook}' failed for plugin '{plugin}': {reason}")]
108    HookFailed {
109        plugin: String,
110        hook: String,
111        reason: String,
112    },
113}
114
115/// 中间件错误
116#[derive(Debug, Error)]
117pub enum MiddlewareError {
118    #[error("Middleware '{name}' execution failed: {reason}")]
119    ExecutionFailed { name: String, reason: String },
120
121    #[error("Middleware chain interrupted by '{name}': {reason}")]
122    ChainInterrupted { name: String, reason: String },
123
124    #[error("Middleware '{name}' not found")]
125    NotFound { name: String },
126}
127
128/// 指标收集错误
129#[derive(Debug, Error)]
130pub enum MetricsError {
131    #[error("Failed to collect metrics: {0}")]
132    CollectionFailed(String),
133
134    #[error("Failed to export metrics: {0}")]
135    ExportFailed(String),
136
137    #[error("Invalid metric name: {0}")]
138    InvalidName(String),
139}