Skip to main content

mf_collab_client/mapping_v2/
error.rs

1use thiserror::Error;
2use std::any::TypeId;
3
4/// 转换错误类型 - 提供详细的错误信息和上下文
5#[derive(Error, Debug, Clone)]
6pub enum ConversionError {
7    #[error("不支持的步骤类型: {step_type} (TypeId: {type_id:?})")]
8    UnsupportedStepType { step_type: String, type_id: TypeId },
9
10    #[error("步骤验证失败: {reason}, 步骤: {step_name}")]
11    ValidationFailed { step_name: String, reason: String },
12
13    #[error("Yrs 事务操作失败: {operation}, 原因: {reason}")]
14    YrsTransactionFailed { operation: String, reason: String },
15
16    #[error("节点操作失败: {node_id}, 操作: {operation}, 原因: {reason}")]
17    NodeOperationFailed { node_id: String, operation: String, reason: String },
18
19    #[error("属性操作失败: 节点 {node_id}, 属性 {attr_key}, 原因: {reason}")]
20    AttributeOperationFailed {
21        node_id: String,
22        attr_key: String,
23        reason: String,
24    },
25
26    #[error(
27        "标记操作失败: 节点 {node_id}, 标记类型 {mark_type}, 原因: {reason}"
28    )]
29    MarkOperationFailed { node_id: String, mark_type: String, reason: String },
30
31    #[error("序列化失败: {reason}")]
32    SerializationFailed { reason: String },
33
34    #[error(
35        "权限不足: 用户 {user_id} 无法执行操作 {operation} 在节点 {node_id}"
36    )]
37    PermissionDenied { user_id: String, operation: String, node_id: String },
38
39    #[error(
40        "并发冲突: 节点 {node_id} 在客户端 {local_client} 和 {remote_client} 之间存在冲突"
41    )]
42    ConcurrencyConflict {
43        node_id: String,
44        local_client: String,
45        remote_client: String,
46    },
47
48    #[error("自定义错误: {message}")]
49    Custom { message: String },
50}
51
52impl ConversionError {
53    /// 创建不支持步骤类型错误
54    pub fn unsupported_step<T: 'static>(step_name: &str) -> Self {
55        Self::UnsupportedStepType {
56            step_type: step_name.to_string(),
57            type_id: TypeId::of::<T>(),
58        }
59    }
60
61    /// 创建验证失败错误
62    pub fn validation_failed(
63        step_name: &str,
64        reason: &str,
65    ) -> Self {
66        Self::ValidationFailed {
67            step_name: step_name.to_string(),
68            reason: reason.to_string(),
69        }
70    }
71
72    /// 创建节点操作失败错误
73    pub fn node_operation_failed(
74        node_id: &str,
75        operation: &str,
76        reason: &str,
77    ) -> Self {
78        Self::NodeOperationFailed {
79            node_id: node_id.to_string(),
80            operation: operation.to_string(),
81            reason: reason.to_string(),
82        }
83    }
84
85    /// 创建权限不足错误
86    pub fn permission_denied(
87        user_id: &str,
88        operation: &str,
89        node_id: &str,
90    ) -> Self {
91        Self::PermissionDenied {
92            user_id: user_id.to_string(),
93            operation: operation.to_string(),
94            node_id: node_id.to_string(),
95        }
96    }
97}
98
99/// 转换结果类型别名
100pub type ConversionResult<T> = Result<T, ConversionError>;
101
102/// 可恢复的转换错误 - 支持重试机制
103#[derive(Error, Debug, Clone)]
104pub enum RecoverableError {
105    #[error("临时网络错误: {reason}")]
106    TemporaryNetworkError { reason: String },
107
108    #[error("资源暂时不可用: {resource}")]
109    ResourceTemporarilyUnavailable { resource: String },
110
111    #[error("事务冲突,可重试: {reason}")]
112    RetryableTransactionConflict { reason: String },
113}
114
115impl From<RecoverableError> for ConversionError {
116    fn from(err: RecoverableError) -> Self {
117        Self::Custom { message: err.to_string() }
118    }
119}