tower_abci/buffer4/
error.rs

1//! Error types for the `Buffer` middleware.
2
3use crate::BoxError;
4use 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)]
11pub struct ServiceError {
12    inner: Arc<BoxError>,
13}
14
15/// An error produced when the a buffer's worker closes unexpectedly.
16pub struct Closed {
17    _p: (),
18}
19
20// ===== impl ServiceError =====
21
22impl ServiceError {
23    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
29    pub(crate) fn clone(&self) -> ServiceError {
30        ServiceError {
31            inner: self.inner.clone(),
32        }
33    }
34}
35
36impl fmt::Display for ServiceError {
37    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
38        write!(fmt, "buffered service failed: {}", self.inner)
39    }
40}
41
42impl std::error::Error for ServiceError {
43    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
44        Some(&**self.inner)
45    }
46}
47
48// ===== impl Closed =====
49
50impl Closed {
51    pub(crate) fn new() -> Self {
52        Closed { _p: () }
53    }
54}
55
56impl fmt::Debug for Closed {
57    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
58        fmt.debug_tuple("Closed").finish()
59    }
60}
61
62impl fmt::Display for Closed {
63    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
64        fmt.write_str("buffer's worker closed unexpectedly")
65    }
66}
67
68impl std::error::Error for Closed {}