async_mq/
error.rs

1// SPDX-License-Identifier: Apache-2.0 AND MIT
2//! `Error` enum type
3
4/// An error enum.
5pub enum Error {
6    /// [lapin::Error] variant.
7    ///
8    /// [lapin::Error]: https://docs.rs/lapin/latest/lapin/enum.Error.html
9    Internal(lapin::Error),
10    /// Other error variant.
11    Other,
12}
13
14impl std::error::Error for Error {
15    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
16        match self {
17            Self::Internal(err) => Some(err),
18            Self::Other => None,
19        }
20    }
21}
22
23impl std::fmt::Display for Error {
24    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
25        match self {
26            Self::Internal(err) => err.fmt(f),
27            Self::Other => write!(f, "other error"),
28        }
29    }
30}
31
32impl std::fmt::Debug for Error {
33    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34        match self {
35            Self::Internal(err) => err.fmt(f),
36            Self::Other => write!(f, "Error::Other"),
37        }
38    }
39}
40
41impl From<lapin::Error> for Error {
42    fn from(err: lapin::Error) -> Self {
43        Self::Internal(err)
44    }
45}
46
47impl std::cmp::PartialEq for Error {
48    fn eq(&self, other: &Self) -> bool {
49        match self {
50            Self::Internal(err) => match other {
51                Self::Internal(other) => Self::eq_internal(err, other),
52                _ => false,
53            },
54            Self::Other => match other {
55                Self::Other => true,
56                _ => false,
57            },
58        }
59    }
60}
61
62type LapinErr = lapin::Error;
63
64impl Error {
65    fn eq_internal(a: &lapin::Error, b: &lapin::Error) -> bool {
66        match a {
67            LapinErr::InvalidMethod(a) => match b {
68                LapinErr::InvalidMethod(b) => a == b,
69                _ => false,
70            },
71            LapinErr::InvalidChannel(a) => match b {
72                LapinErr::InvalidChannel(b) => a == b,
73                _ => false,
74            },
75            LapinErr::InvalidAck => match b {
76                LapinErr::InvalidAck => true,
77                _ => false,
78            },
79            LapinErr::InvalidBodyReceived => match b {
80                LapinErr::InvalidBodyReceived => true,
81                _ => false,
82            },
83            LapinErr::InvalidFrameReceived => match b {
84                LapinErr::InvalidFrameReceived => true,
85                _ => false,
86            },
87            LapinErr::UnexpectedReply => match b {
88                LapinErr::UnexpectedReply => true,
89                _ => false,
90            },
91            LapinErr::ChannelsLimitReached => match b {
92                LapinErr::ChannelsLimitReached => true,
93                _ => false,
94            },
95            LapinErr::InvalidChannelState(a) => match b {
96                LapinErr::InvalidChannelState(b) => a == b,
97                _ => false,
98            },
99            LapinErr::InvalidConnectionState(a) => match b {
100                LapinErr::InvalidConnectionState(b) => a == b,
101                _ => false,
102            },
103            LapinErr::ParsingError(a) => match b {
104                LapinErr::ParsingError(b) => a == b,
105                _ => false,
106            },
107            LapinErr::SerialisationError(a) => match b {
108                LapinErr::SerialisationError(b) => Self::eq_gen_error(a, b),
109                _ => false,
110            },
111            LapinErr::IOError(a) => match b {
112                LapinErr::IOError(b) => a.kind() == b.kind(),
113                _ => false,
114            },
115            LapinErr::ProtocolError(a) => match b {
116                LapinErr::ProtocolError(b) => a == b,
117                _ => false,
118            },
119            LapinErr::__Nonexhaustive => match b {
120                LapinErr::__Nonexhaustive => true,
121                _ => false,
122            },
123        }
124    }
125    fn eq_gen_error(a: &cookie_factory::GenError, b: &cookie_factory::GenError) -> bool {
126        match a {
127            cookie_factory::GenError::BufferTooSmall(a) => match b {
128                cookie_factory::GenError::BufferTooSmall(b) => a == b,
129                _ => false,
130            },
131            cookie_factory::GenError::BufferTooBig(a) => match b {
132                cookie_factory::GenError::BufferTooBig(b) => a == b,
133                _ => false,
134            },
135            cookie_factory::GenError::InvalidOffset => match b {
136                cookie_factory::GenError::InvalidOffset => true,
137                _ => false,
138            },
139            cookie_factory::GenError::IoError(a) => match b {
140                cookie_factory::GenError::IoError(b) => a.kind() == b.kind(),
141                _ => false,
142            },
143            cookie_factory::GenError::CustomError(a) => match b {
144                cookie_factory::GenError::CustomError(b) => a == b,
145                _ => false,
146            },
147            cookie_factory::GenError::NotYetImplemented => match b {
148                cookie_factory::GenError::NotYetImplemented => true,
149                _ => false,
150            },
151        }
152    }
153}
154
155#[cfg(test)]
156mod tests {
157    use cookie_factory;
158    use lapin;
159    use std::error::Error;
160    use std::fmt::Write;
161    use std::io::{self, ErrorKind};
162    #[test]
163    fn source_internal_then_debug() {
164        struct Test {
165            data: super::Error,
166            want: String,
167        }
168        let mut tests = [
169            Test {
170                data: crate::Error::Internal(lapin::Error::UnexpectedReply),
171                want: String::from("UnexpectedReply"),
172            },
173            Test {
174                data: crate::Error::Internal(lapin::Error::ChannelsLimitReached),
175                want: String::from("ChannelsLimitReached"),
176            },
177        ];
178        for t in &mut tests {
179            let mut got = String::new();
180            let err = t.data.source().unwrap();
181            write!(&mut got, "{:?}", err).unwrap();
182            assert_eq!(t.want, got);
183        }
184    }
185    #[test]
186    fn debug_internal() {
187        struct Test {
188            data: super::Error,
189            want: String,
190        }
191        let mut tests = [
192            Test {
193                data: crate::Error::Internal(lapin::Error::UnexpectedReply),
194                want: String::from("UnexpectedReply"),
195            },
196            Test {
197                data: crate::Error::Internal(lapin::Error::ChannelsLimitReached),
198                want: String::from("ChannelsLimitReached"),
199            },
200        ];
201        for t in &mut tests {
202            let mut got = String::new();
203            write!(&mut got, "{:?}", t.data).unwrap();
204            assert_eq!(t.want, got);
205        }
206    }
207    #[test]
208    fn from_internal() {
209        use std::sync::Arc;
210        struct Test {
211            data: Option<lapin::Error>,
212            want: crate::Error,
213        }
214        let mut tests = [
215            Test {
216                data: Some(lapin::Error::UnexpectedReply),
217                want: crate::Error::Internal(lapin::Error::UnexpectedReply),
218            },
219            Test {
220                data: Some(lapin::Error::ChannelsLimitReached),
221                want: crate::Error::Internal(lapin::Error::ChannelsLimitReached),
222            },
223            Test {
224                data: Some(lapin::Error::InvalidChannelState(
225                    lapin::ChannelState::Initial,
226                )),
227                want: crate::Error::Internal(lapin::Error::InvalidChannelState(
228                    lapin::ChannelState::Initial,
229                )),
230            },
231            Test {
232                data: Some(lapin::Error::InvalidChannelState(
233                    lapin::ChannelState::Connected,
234                )),
235                want: crate::Error::Internal(lapin::Error::InvalidChannelState(
236                    lapin::ChannelState::Connected,
237                )),
238            },
239            Test {
240                data: Some(lapin::Error::InvalidChannelState(
241                    lapin::ChannelState::Closing,
242                )),
243                want: crate::Error::Internal(lapin::Error::InvalidChannelState(
244                    lapin::ChannelState::Closing,
245                )),
246            },
247            Test {
248                data: Some(lapin::Error::InvalidChannelState(
249                    lapin::ChannelState::Closed,
250                )),
251                want: crate::Error::Internal(lapin::Error::InvalidChannelState(
252                    lapin::ChannelState::Closed,
253                )),
254            },
255            Test {
256                data: Some(lapin::Error::InvalidChannelState(
257                    lapin::ChannelState::Error,
258                )),
259                want: crate::Error::Internal(lapin::Error::InvalidChannelState(
260                    lapin::ChannelState::Error,
261                )),
262            },
263            Test {
264                data: Some(lapin::Error::InvalidChannelState(
265                    lapin::ChannelState::SendingContent(1024),
266                )),
267                want: crate::Error::Internal(lapin::Error::InvalidChannelState(
268                    lapin::ChannelState::SendingContent(1024),
269                )),
270            },
271            Test {
272                data: Some(lapin::Error::InvalidConnectionState(
273                    lapin::ConnectionState::Initial,
274                )),
275                want: crate::Error::Internal(lapin::Error::InvalidConnectionState(
276                    lapin::ConnectionState::Initial,
277                )),
278            },
279            Test {
280                data: Some(lapin::Error::InvalidConnectionState(
281                    lapin::ConnectionState::Connected,
282                )),
283                want: crate::Error::Internal(lapin::Error::InvalidConnectionState(
284                    lapin::ConnectionState::Connected,
285                )),
286            },
287            Test {
288                data: Some(lapin::Error::InvalidConnectionState(
289                    lapin::ConnectionState::Closing,
290                )),
291                want: crate::Error::Internal(lapin::Error::InvalidConnectionState(
292                    lapin::ConnectionState::Closing,
293                )),
294            },
295            Test {
296                data: Some(lapin::Error::InvalidConnectionState(
297                    lapin::ConnectionState::Closed,
298                )),
299                want: crate::Error::Internal(lapin::Error::InvalidConnectionState(
300                    lapin::ConnectionState::Closed,
301                )),
302            },
303            Test {
304                data: Some(lapin::Error::InvalidConnectionState(
305                    lapin::ConnectionState::Error,
306                )),
307                want: crate::Error::Internal(lapin::Error::InvalidConnectionState(
308                    lapin::ConnectionState::Error,
309                )),
310            },
311            Test {
312                data: Some(lapin::Error::SerialisationError(Arc::new(
313                    cookie_factory::GenError::BufferTooSmall(1),
314                ))),
315                want: crate::Error::Internal(lapin::Error::SerialisationError(Arc::new(
316                    cookie_factory::GenError::BufferTooSmall(1),
317                ))),
318            },
319            Test {
320                data: Some(lapin::Error::SerialisationError(Arc::new(
321                    cookie_factory::GenError::BufferTooBig(1024 * 1024 * 1024),
322                ))),
323                want: crate::Error::Internal(lapin::Error::SerialisationError(Arc::new(
324                    cookie_factory::GenError::BufferTooBig(1024 * 1024 * 1024),
325                ))),
326            },
327            Test {
328                data: Some(lapin::Error::SerialisationError(Arc::new(
329                    cookie_factory::GenError::InvalidOffset,
330                ))),
331                want: crate::Error::Internal(lapin::Error::SerialisationError(Arc::new(
332                    cookie_factory::GenError::InvalidOffset,
333                ))),
334            },
335            Test {
336                data: Some(lapin::Error::SerialisationError(Arc::new(
337                    cookie_factory::GenError::IoError(cookie_factory::lib::std::io::Error::new(
338                        io::ErrorKind::NotFound,
339                        "not found",
340                    )),
341                ))),
342                want: crate::Error::Internal(lapin::Error::SerialisationError(Arc::new(
343                    cookie_factory::GenError::IoError(cookie_factory::lib::std::io::Error::new(
344                        io::ErrorKind::NotFound,
345                        "not found",
346                    )),
347                ))),
348            },
349            Test {
350                data: Some(lapin::Error::IOError(Arc::new(io::Error::new(
351                    ErrorKind::Interrupted,
352                    "interrupted",
353                )))),
354                want: crate::Error::Internal(lapin::Error::IOError(Arc::new(io::Error::new(
355                    ErrorKind::Interrupted,
356                    "interrupted",
357                )))),
358            },
359        ];
360        for t in &mut tests {
361            let err = t.data.take().unwrap();
362            let got = crate::Error::from(err);
363            assert_eq!(t.want, got);
364        }
365    }
366}