1pub 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 pub fn serialize(msg: String) -> Self {
41 Error {
42 inner: ErrorKind::SerializeError(msg),
43 }
44 }
45
46 pub fn deserialize(msg: String) -> Self {
50 Error {
51 inner: ErrorKind::DeserializeError(msg),
52 }
53 }
54
55 pub fn validate(value: i64, msg: String) -> Self {
59 Error {
60 inner: ErrorKind::ValidateError(value, msg),
61 }
62 }
63}
64
65pub enum ErrorKind {
69 SerializeError(String),
73
74 DeserializeError(String),
78
79 ValidateError(i64, String),
83}
84
85pub 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 pub fn new(code: i64, message: String) -> Self {
120 ValidateError { code, message }
121 }
122
123 pub fn to_afastdata_error(&self) -> Error {
127 Error::validate(self.code, self.message.clone())
128 }
129}