1#![allow(deprecated)]
2use std::{
4 error::Error as StdError,
5 marker::PhantomData,
6 pin::Pin,
7 task::{Context, Poll},
8 time::Duration,
9};
10use thiserror::Error;
11use tower_service::Service;
12
13pub type BoxDynError = Box<dyn StdError + 'static + Send + Sync>;
15#[derive(Error, Debug)]
18#[error("AbortError: {source}")]
19pub struct AbortError {
20 #[source]
21 source: BoxDynError,
22}
23impl AbortError {
24 pub fn new<E: Into<BoxDynError>>(err: E) -> Self {
26 Self { source: err.into() }
27 }
28}
29
30#[derive(Error, Debug)]
33#[error("RetryAfterError: {source}, duration: {duration:?}")]
34pub struct RetryAfterError {
35 #[source]
36 source: BoxDynError,
37 duration: Duration,
38}
39
40impl RetryAfterError {
41 pub fn new<E: Into<BoxDynError>>(err: E, duration: Duration) -> Self {
43 Self {
44 source: err.into(),
45 duration,
46 }
47 }
48
49 #[must_use]
51 pub fn get_duration(&self) -> Duration {
52 self.duration
53 }
54}
55
56#[deprecated(
58 since = "1.1.1",
59 note = "This error type is deprecated and will be removed in a future release. Returning a `BoxDynError` or a custom error directly is now the preferred way to indicate that a task should be retried."
60)]
61#[allow(deprecated)]
62#[derive(Error, Debug)]
63#[error("DeferredError: {source}")]
64pub struct DeferredError {
65 #[source]
66 source: BoxDynError,
67}
68
69#[allow(deprecated)]
70impl DeferredError {
71 pub fn new<E: Into<BoxDynError>>(source: E) -> Self {
73 Self {
74 source: source.into(),
75 }
76 }
77}
78
79#[non_exhaustive]
81#[derive(Error, Debug)]
82pub enum WorkerError {
83 #[error("Failed to poll backend: {0}")]
85 BackendError(BoxDynError),
86 #[error("Failed to handle the new state: {0}")]
88 StateError(#[from] WorkerStateError),
89 #[error("Worker panicked: {0}")]
91 PanicError(String),
92 #[error("IO error: {0}")]
94 IoError(#[from] std::io::Error),
95}
96
97#[derive(Error, Debug)]
99#[non_exhaustive]
100pub enum WorkerStateError {
101 #[error("Worker not started, did you forget to call worker.start()")]
103 NotStarted,
104 #[error("Worker already started")]
106 AlreadyStarted,
107 #[error("Worker is not running")]
109 NotRunning,
110 #[error("Worker is not paused")]
112 NotPaused,
113 #[error("Worker is shutting down")]
115 ShuttingDown,
116 #[error("Worker provided with invalid state {0}")]
118 InvalidState(String),
119}
120
121#[derive(Clone, Debug)]
126pub struct ErrorHandlingLayer {
127 _p: PhantomData<()>,
128}
129
130impl ErrorHandlingLayer {
131 #[must_use]
133 pub fn new() -> Self {
134 Self { _p: PhantomData }
135 }
136}
137
138impl Default for ErrorHandlingLayer {
139 fn default() -> Self {
140 Self::new()
141 }
142}
143
144impl<S> tower_layer::Layer<S> for ErrorHandlingLayer {
145 type Service = ErrorHandlingService<S>;
146
147 fn layer(&self, service: S) -> Self::Service {
148 ErrorHandlingService { service }
149 }
150}
151
152#[derive(Clone, Debug)]
154pub struct ErrorHandlingService<S> {
155 service: S,
156}
157
158impl<S, Request> Service<Request> for ErrorHandlingService<S>
159where
160 S: Service<Request>,
161 S::Error: std::error::Error + Send + Sync + 'static,
162 S::Future: Send + 'static,
163{
164 type Response = S::Response;
165 type Error = BoxDynError;
166 type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;
167
168 fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
169 self.service.poll_ready(cx).map_err(|e| {
170 let boxed_error: BoxDynError = e.into();
171 boxed_error
172 })
173 }
174
175 fn call(&mut self, req: Request) -> Self::Future {
176 let fut = self.service.call(req);
177
178 Box::pin(async move {
179 fut.await.map_err(|e| {
180 let boxed_error: BoxDynError = e.into();
181 boxed_error
182 })
183 })
184 }
185}