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