1 //! Error types for the `Buffer` middleware.
2 
3 use crate::BoxError;
4 use std::{fmt, sync::Arc};
5 
6 /// An error produced by a [`Service`] wrapped by a [`Buffer`]
7 ///
8 /// [`Service`]: crate::Service
9 /// [`Buffer`]: crate::buffer::Buffer
10 #[derive(Debug)]
11 pub struct ServiceError {
12     inner: Arc<BoxError>,
13 }
14 
15 /// An error produced when the a buffer's worker closes unexpectedly.
16 pub struct Closed {
17     _p: (),
18 }
19 
20 // ===== impl ServiceError =====
21 
22 impl ServiceError {
new(inner: BoxError) -> ServiceError23     pub(crate) fn new(inner: BoxError) -> ServiceError {
24         let inner = Arc::new(inner);
25         ServiceError { inner }
26     }
27 
28     // Private to avoid exposing `Clone` trait as part of the public API
clone(&self) -> ServiceError29     pub(crate) fn clone(&self) -> ServiceError {
30         ServiceError {
31             inner: self.inner.clone(),
32         }
33     }
34 }
35 
36 impl fmt::Display for ServiceError {
fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result37     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
38         write!(fmt, "buffered service failed: {}", self.inner)
39     }
40 }
41 
42 impl std::error::Error for ServiceError {
source(&self) -> Option<&(dyn std::error::Error + 'static)>43     fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
44         Some(&**self.inner)
45     }
46 }
47 
48 // ===== impl Closed =====
49 
50 impl Closed {
new() -> Self51     pub(crate) fn new() -> Self {
52         Closed { _p: () }
53     }
54 }
55 
56 impl fmt::Debug for Closed {
fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result57     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
58         fmt.debug_tuple("Closed").finish()
59     }
60 }
61 
62 impl fmt::Display for Closed {
fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result63     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
64         fmt.write_str("buffer's worker closed unexpectedly")
65     }
66 }
67 
68 impl std::error::Error for Closed {}
69