moduforge_core/
error.rs

1use std::fmt;
2use std::io;
3use crate::flow::FlowError;
4
5/// 编辑器核心错误类型
6#[derive(Debug)]
7pub enum EditorError {
8    /// 状态相关错误
9    StateError(String),
10    /// 存储相关错误
11    StorageError(String),
12    /// 事件处理错误
13    EventError(String),
14    /// 插件相关错误
15    PluginError(String),
16    /// IO操作错误
17    IoError(io::Error),
18    /// 配置错误
19    ConfigError(String),
20    /// 事务处理错误
21    TransactionError(String),
22    /// 历史记录错误
23    HistoryError(String),
24    /// 引擎错误
25    EngineError(String),
26    /// 缓存错误
27    CacheError(String),
28    /// 未知错误
29    Unknown(String),
30    /// 其他错误
31    Other(String),
32    /// 流程错误
33    Flow(FlowError),
34}
35
36impl fmt::Display for EditorError {
37    fn fmt(
38        &self,
39        f: &mut fmt::Formatter<'_>,
40    ) -> fmt::Result {
41        match self {
42            EditorError::StateError(msg) => write!(f, "State error: {}", msg),
43            EditorError::StorageError(msg) => {
44                write!(f, "Storage error: {}", msg)
45            },
46            EditorError::EventError(msg) => write!(f, "Event error: {}", msg),
47            EditorError::PluginError(msg) => write!(f, "Plugin error: {}", msg),
48            EditorError::IoError(err) => write!(f, "IO error: {}", err),
49            EditorError::ConfigError(msg) => write!(f, "Config error: {}", msg),
50            EditorError::TransactionError(msg) => {
51                write!(f, "Transaction error: {}", msg)
52            },
53            EditorError::HistoryError(msg) => {
54                write!(f, "History error: {}", msg)
55            },
56            EditorError::EngineError(msg) => write!(f, "Engine error: {}", msg),
57            EditorError::CacheError(msg) => write!(f, "Cache error: {}", msg),
58            EditorError::Unknown(msg) => write!(f, "Unknown error: {}", msg),
59            EditorError::Other(msg) => write!(f, "Other error: {}", msg),
60            EditorError::Flow(err) => write!(f, "Flow error: {}", err),
61        }
62    }
63}
64
65impl std::error::Error for EditorError {}
66
67impl From<io::Error> for EditorError {
68    fn from(err: io::Error) -> Self {
69        EditorError::IoError(err)
70    }
71}
72
73impl From<String> for EditorError {
74    fn from(err: String) -> Self {
75        EditorError::Unknown(err)
76    }
77}
78
79impl From<&str> for EditorError {
80    fn from(err: &str) -> Self {
81        EditorError::Unknown(err.to_string())
82    }
83}
84
85impl From<FlowError> for EditorError {
86    fn from(err: FlowError) -> Self {
87        EditorError::Flow(err)
88    }
89}
90
91/// 错误结果类型别名
92pub type EditorResult<T> = Result<T, EditorError>;
93
94/// 错误处理工具函数
95pub mod error_utils {
96    use super::*;
97
98    /// 将错误转换为更具体的错误类型
99    pub fn map_error<T, E: std::error::Error>(
100        result: Result<T, E>,
101        context: &str,
102    ) -> EditorResult<T> {
103        result.map_err(|e| EditorError::Unknown(format!("{}: {}", context, e)))
104    }
105
106    /// 创建状态错误
107    pub fn state_error(msg: impl Into<String>) -> EditorError {
108        EditorError::StateError(msg.into())
109    }
110
111    /// 创建存储错误
112    pub fn storage_error(msg: impl Into<String>) -> EditorError {
113        EditorError::StorageError(msg.into())
114    }
115
116    /// 创建事件错误
117    pub fn event_error(msg: impl Into<String>) -> EditorError {
118        EditorError::EventError(msg.into())
119    }
120
121    /// 创建插件错误
122    pub fn plugin_error(msg: impl Into<String>) -> EditorError {
123        EditorError::PluginError(msg.into())
124    }
125
126    /// 创建配置错误
127    pub fn config_error(msg: impl Into<String>) -> EditorError {
128        EditorError::ConfigError(msg.into())
129    }
130
131    /// 创建事务错误
132    pub fn transaction_error(msg: impl Into<String>) -> EditorError {
133        EditorError::TransactionError(msg.into())
134    }
135
136    /// 创建历史记录错误
137    pub fn history_error(msg: impl Into<String>) -> EditorError {
138        EditorError::HistoryError(msg.into())
139    }
140
141    /// 创建引擎错误
142    pub fn engine_error(msg: impl Into<String>) -> EditorError {
143        EditorError::EngineError(msg.into())
144    }
145
146    /// 创建缓存错误
147    pub fn cache_error(msg: impl Into<String>) -> EditorError {
148        EditorError::CacheError(msg.into())
149    }
150
151    /// 创建中间件错误
152    pub fn middleware_error(msg: impl Into<String>) -> EditorError {
153        EditorError::PluginError(msg.into())
154    }
155}