tower_buffer/
error.rs

1//! Error types
2
3use std::{fmt, sync::Arc};
4
5/// An error produced by a `Service` wrapped by a `Buffer`
6#[derive(Debug)]
7pub struct ServiceError {
8    inner: Arc<Error>,
9}
10
11/// An error when the buffer's worker closes unexpectedly.
12#[derive(Debug)]
13pub struct Closed {
14    _p: (),
15}
16
17/// Errors produced by `Buffer`.
18pub(crate) type Error = Box<dyn std::error::Error + Send + Sync>;
19
20// ===== impl ServiceError =====
21
22impl ServiceError {
23    pub(crate) fn new(inner: Error) -> 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::Display for Closed {
57    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
58        fmt.write_str("buffer's worker closed unexpectedly")
59    }
60}
61
62impl std::error::Error for Closed {}