Skip to main content

afastdata_core/
error.rs

1/// afastdata-core 的核心错误类型。
2///
3/// Core error type for afastdata-core.
4///
5/// Encapsulates serialization, deserialization, and validation failures.
6pub struct Error {
7    inner: ErrorKind,
8}
9
10impl std::fmt::Display for Error {
11    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
12        match &self.inner {
13            ErrorKind::SerializeError(msg) => write!(f, "Serialize error: {}", msg),
14            ErrorKind::DeserializeError(msg) => write!(f, "Deserialize error: {}", msg),
15            ErrorKind::ValidateError(code, msg) => {
16                write!(f, "Validation error: code {} is invalid: {}", code, msg)
17            }
18        }
19    }
20}
21
22impl std::fmt::Debug for Error {
23    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24        match &self.inner {
25            ErrorKind::SerializeError(msg) => write!(f, "Serialize error: {}", msg),
26            ErrorKind::DeserializeError(msg) => write!(f, "Deserialize error: {}", msg),
27            ErrorKind::ValidateError(code, msg) => {
28                write!(f, "Validation error: code {} is invalid: {}", code, msg)
29            }
30        }
31    }
32}
33
34impl std::error::Error for Error {}
35
36impl Error {
37    /// 创建一个序列化错误。
38    ///
39    /// Creates a serialization error.
40    pub fn serialize(msg: String) -> Self {
41        Error {
42            inner: ErrorKind::SerializeError(msg),
43        }
44    }
45
46    /// 创建一个反序列化错误。
47    ///
48    /// Creates a deserialization error.
49    pub fn deserialize(msg: String) -> Self {
50        Error {
51            inner: ErrorKind::DeserializeError(msg),
52        }
53    }
54
55    /// 创建一个验证错误,包含无效值和错误信息。
56    ///
57    /// Creates a validation error with the invalid value and a message.
58    pub fn validate(value: i64, msg: String) -> Self {
59        Error {
60            inner: ErrorKind::ValidateError(value, msg),
61        }
62    }
63}
64
65/// 列举 afastdata-core 可能产生的错误类型。
66///
67/// Enumerates the kinds of errors produced by afastdata-core.
68pub enum ErrorKind {
69    /// 序列化失败,携带文本消息。
70    ///
71    /// Serialization failure with a textual message.
72    SerializeError(String),
73
74    /// 反序列化失败,携带文本消息。
75    ///
76    /// Deserialization failure with a textual message.
77    DeserializeError(String),
78    
79    /// 验证失败,包含无效值和错误信息。
80    ///
81    /// Validation failure, carrying the invalid value and a message.
82    ValidateError(i64, String),
83}
84
85/// 用于更丰富错误构造的验证错误类型。
86///
87/// Validation-specific error type used for richer error construction.
88pub struct ValidateError {
89    code: i64,
90    message: String,
91}
92
93impl std::fmt::Display for ValidateError {
94    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
95        write!(
96            f,
97            "Validation error: code {}, message {}",
98            self.code, self.message
99        )
100    }
101}
102
103impl std::fmt::Debug for ValidateError {
104    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
105        write!(
106            f,
107            "Validation error: code {}, message {}",
108            self.code, self.message
109        )
110    }
111}
112
113impl std::error::Error for ValidateError {}
114
115impl ValidateError {
116    /// 创建一个新的 `ValidateError`。
117    ///
118    /// Creates a new `ValidateError`.
119    pub fn new(code: i64, message: String) -> Self {
120        ValidateError { code, message }
121    }
122
123    /// 将此验证错误转换为通用 `Error` 类型。
124    ///
125    /// Converts this validation error into the generic `Error` type.
126    pub fn to_afastdata_error(&self) -> Error {
127        Error::validate(self.code, self.message.clone())
128    }
129}