moduforge_runtime/
error.rs

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