1use std::env::VarError;
2use std::net::AddrParseError;
3
4use snafu::Snafu;
5
6use crate::DataLenType;
7use crate::checksum::ChecksumType;
8
9#[derive(Debug, Snafu)]
10#[snafu(visibility(pub))]
13pub enum Error {
14 #[snafu(display("read `checksum` from network error"))]
16 MsgNetworkReadCheckSum { source: std::io::Error },
17 #[snafu(display("read `buffered_raw_data` from network error"))]
18 MsgNetworkReadBufferdRawData { source: std::io::Error },
19 #[snafu(display("read `msg_body` from network error"))]
20 MsgNetworkReadBody { source: std::io::Error },
21 #[snafu(display("write `checksum` to network error"))]
22 MsgNetworkWriteCheckSum { source: std::io::Error },
23 #[snafu(display("write `datalen` to network error"))]
24 MsgNetworkWriteDatalen { source: std::io::Error },
25 #[snafu(display("write `msg_body` to network error"))]
26 MsgNetworkWriteBody { source: std::io::Error },
27 #[snafu(display("write `codec_msg` to network error"))]
28 MsgNetworkWriteCodecMsg { source: std::io::Error },
29 #[snafu(display("write `codec_tag` to network error"))]
30 MsgNetworkWriteCodecTag { source: std::io::Error },
31 #[snafu(display(
32 "`datalen` exceeded! the length must be less than {max}, but the actual length is {actual}"
33 ))]
34 MsgDatalenExceeded {
35 actual: DataLenType,
36 max: DataLenType,
37 },
38 #[snafu(display(
39 "`datalen` not valid,`datalen:{datalen}` doesn't pass the `checksum:{checksum}`"
40 ))]
41 MsgDatalenValidate {
42 datalen: DataLenType,
43 checksum: ChecksumType,
44 },
45 #[snafu(display("{action} `{struct_name}` error with content:{content}"))]
46 MsgSerialize {
47 action: &'static str,
49 struct_name: &'static str,
51 content: String,
53 source: serde_json::Error,
54 },
55 #[snafu(display("`{action}` failed with the specific error: `{detail}`"))]
56 MsgCodec {
57 action: &'static str,
59 detail: String,
61 },
62 #[snafu(display("protocol-v2 error: {detail}"))]
63 MsgProtocol { detail: String },
64 #[snafu(display("`{action}` forward message failed: {source}"))]
65 MsgForward {
66 action: &'static str,
68 source: std::io::Error,
69 },
70 #[snafu(display("failed to forward message to write in normal text"))]
72 FwdNetworkWriteWithNormal { source: std::io::Error },
73 #[snafu(display("parse socket address from string:`{string}` error"))]
75 CfgParseSockAddr {
76 string: String,
77 source: AddrParseError,
78 },
79 #[snafu(display("failed to resolve `{string}`: {source}"))]
82 CfgResolveSockAddr {
83 string: String,
84 source: std::io::Error,
85 },
86 #[snafu(display("`PB_MAPPER_SERVER` env not set"))]
87 CfgPbServerEnvNotExist { source: VarError },
88}
89
90pub type Result<T, E = Error> = std::result::Result<T, E>;
91
92impl Error {
93 pub fn is_expected_disconnect(&self) -> bool {
94 use std::io::ErrorKind;
95
96 let is_expected = |kind: ErrorKind| {
97 matches!(
98 kind,
99 ErrorKind::UnexpectedEof
100 | ErrorKind::ConnectionReset
101 | ErrorKind::ConnectionAborted
102 | ErrorKind::BrokenPipe
103 | ErrorKind::NotConnected
104 | ErrorKind::TimedOut
105 )
106 };
107
108 match self {
109 Error::MsgNetworkReadCheckSum { source }
110 | Error::MsgNetworkReadBufferdRawData { source }
111 | Error::MsgNetworkReadBody { source }
112 | Error::MsgNetworkWriteCheckSum { source }
113 | Error::MsgNetworkWriteDatalen { source }
114 | Error::MsgNetworkWriteBody { source }
115 | Error::MsgNetworkWriteCodecMsg { source }
116 | Error::MsgNetworkWriteCodecTag { source }
117 | Error::FwdNetworkWriteWithNormal { source }
118 | Error::MsgForward { source, .. } => is_expected(source.kind()),
119 Error::MsgDatalenExceeded { .. }
120 | Error::MsgDatalenValidate { .. }
121 | Error::MsgSerialize { .. }
122 | Error::MsgCodec { .. }
123 | Error::MsgProtocol { .. }
124 | Error::CfgParseSockAddr { .. }
125 | Error::CfgResolveSockAddr { .. }
126 | Error::CfgPbServerEnvNotExist { .. } => false,
127 }
128 }
129}
130
131#[macro_export]
132macro_rules! snafu_error_handle {
133 ($func_call:expr) => {
134 if let Err(e) = $func_call {
135 tracing::error!("{}", snafu::Report::from_error(e));
136 }
137 };
138 ($func_call:expr, $msg:expr) => {
139 if let Err(e) = $func_call {
140 tracing::error!("{},detail:{}", $msg, snafu::Report::from_error(e));
141 }
142 };
143}
144
145#[macro_export]
146macro_rules! snafu_error_get_or_continue {
147 ($func_call:expr) => {
148 match $func_call {
149 Ok(v) => v,
150 Err(e) => {
151 tracing::error!("{}", snafu::Report::from_error(e));
152 continue;
153 }
154 }
155 };
156 ($func_call:expr, $msg:expr) => {
157 match $func_call {
158 Ok(v) => v,
159 Err(e) => {
160 tracing::error!("{},detail:{}", $msg, snafu::Report::from_error(e));
161 continue;
162 }
163 }
164 };
165}
166
167#[macro_export]
168macro_rules! snafu_error_get_or_return {
169 ($func_call:expr) => {
170 match $func_call {
171 Ok(v) => v,
172 Err(e) => {
173 tracing::error!("{}", snafu::Report::from_error(e));
174 return;
175 }
176 }
177 };
178 ($func_call:expr, $msg:expr) => {
179 match $func_call {
180 Ok(v) => v,
181 Err(e) => {
182 tracing::error!("{},detail:{}", $msg, snafu::Report::from_error(e));
183 return;
184 }
185 }
186 };
187 ($func_call:expr, $msg:expr, $ret_val:expr) => {
188 match $func_call {
189 Ok(v) => v,
190 Err(e) => {
191 tracing::error!("{},detail:{}", $msg, snafu::Report::from_error(e));
192 return $ret_val;
193 }
194 }
195 };
196}
197
198#[macro_export]
199macro_rules! snafu_error_get_or_return_ok {
200 ($func_call:expr) => {
201 match $func_call {
202 Ok(v) => v,
203 Err(e) => {
204 tracing::error!("{}", snafu::Report::from_error(e));
205 return Ok(());
206 }
207 }
208 };
209 ($func_call:expr, $msg:expr) => {
210 match $func_call {
211 Ok(v) => v,
212 Err(e) => {
213 tracing::error!("{},detail:{}", $msg, snafu::Report::from_error(e));
214 return Ok(());
215 }
216 }
217 };
218}