moduforge_core/
error.rs

1use anyhow::{Result};
2/// 错误结果类型别名
3pub type ForgeResult<T> = Result<T>;
4
5/// 错误处理工具函数
6pub mod error_utils {
7    use super::*;
8
9    /// 将错误转换为更具体的错误类型
10    pub fn map_error<T, E: std::error::Error>(
11        result: Result<T, E>,
12        context: &str,
13    ) -> ForgeResult<T> {
14        result.map_err(|e| {
15            anyhow::anyhow!(format!("未知的错误 {}: {}", context, e))
16        })
17    }
18
19    /// 创建状态错误
20    pub fn state_error(msg: impl Into<String>) -> anyhow::Error {
21        anyhow::anyhow!(format!("状态错误 {:?}", msg.into()))
22    }
23
24    /// 创建存储错误
25    pub fn storage_error(msg: impl Into<String>) -> anyhow::Error {
26        anyhow::anyhow!(format!("存储相关错误 {:?}", msg.into()))
27    }
28
29    /// 创建事件错误
30    pub fn event_error(msg: impl Into<String>) -> anyhow::Error {
31        anyhow::anyhow!(format!("事件处理错误 {:?}", msg.into()))
32    }
33
34    /// 创建插件错误
35    pub fn plugin_error(msg: impl Into<String>) -> anyhow::Error {
36        anyhow::anyhow!(format!("插件相关错误 {:?}", msg.into()))
37    }
38
39    /// 创建配置错误
40    pub fn config_error(msg: impl Into<String>) -> anyhow::Error {
41        anyhow::anyhow!(format!("配置错误 {:?}", msg.into()))
42    }
43
44    /// 创建事务错误
45    pub fn transaction_error(msg: impl Into<String>) -> anyhow::Error {
46        anyhow::anyhow!(format!(" 事务处理错误 {:?}", msg.into()))
47    }
48
49    /// 创建历史记录错误
50    pub fn history_error(msg: impl Into<String>) -> anyhow::Error {
51        anyhow::anyhow!(format!(" 历史记录错误 {:?}", msg.into()))
52    }
53
54    /// 创建引擎错误
55    pub fn engine_error(msg: impl Into<String>) -> anyhow::Error {
56        anyhow::anyhow!(format!(" 引擎错误 {:?}", msg.into()))
57    }
58
59    /// 创建缓存错误
60    pub fn cache_error(msg: impl Into<String>) -> anyhow::Error {
61        anyhow::anyhow!(format!("  缓存错误 {:?}", msg.into()))
62    }
63
64    /// 创建中间件错误
65    pub fn middleware_error(msg: impl Into<String>) -> anyhow::Error {
66        anyhow::anyhow!(format!("  中间件错误 {:?}", msg.into()))
67    }
68}