tower-resilience-core 0.9.2

Core infrastructure for tower-resilience: events, metrics, and shared utilities
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
//! Common error types for tower-resilience patterns.
//!
//! This module provides [`ResilienceError`], a unified error type that eliminates
//! the need for manual `From` trait implementations when composing multiple resilience
//! layers.
//!
//! # The Problem
//!
//! When using multiple resilience layers (bulkhead, circuit breaker, rate limiter, etc.),
//! you typically need to write repetitive `From` trait implementations:
//!
//! ```rust,ignore
//! // Without ResilienceError: ~80 lines of boilerplate for 4 layers
//! impl From<BulkheadError> for ServiceError { /* ... */ }
//! impl From<CircuitBreakerError> for ServiceError { /* ... */ }
//! impl From<RateLimiterError> for ServiceError { /* ... */ }
//! impl From<TimeLimiterError> for ServiceError { /* ... */ }
//! ```
//!
//! # The Solution
//!
//! Use [`ResilienceError<E>`] as your service error type:
//!
//! ```rust
//! use tower_resilience_core::ResilienceError;
//!
//! // Your application error
//! #[derive(Debug, Clone)]
//! enum AppError {
//!     DatabaseDown,
//!     InvalidRequest,
//! }
//!
//! impl std::fmt::Display for AppError {
//!     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
//!         match self {
//!             AppError::DatabaseDown => write!(f, "Database down"),
//!             AppError::InvalidRequest => write!(f, "Invalid request"),
//!         }
//!     }
//! }
//!
//! impl std::error::Error for AppError {}
//!
//! // That's it! Zero From implementations needed
//! type ServiceError = ResilienceError<AppError>;
//! ```
//!
//! # Benefits
//!
//! - **Zero boilerplate**: No manual `From` implementations
//! - **Works with any number of layers**: Add or remove layers without touching error code
//! - **Rich error context**: Layer names, counts, durations included
//! - **Application errors preserved**: Wrapped in `Application` variant
//! - **Convenient helpers**: `is_timeout()`, `is_rate_limited()`, etc.
//!
//! # Pattern Matching
//!
//! ```rust
//! use tower_resilience_core::ResilienceError;
//! use std::time::Duration;
//!
//! # #[derive(Debug)]
//! # struct AppError;
//! # impl std::fmt::Display for AppError {
//! #     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { Ok(()) }
//! # }
//! # impl std::error::Error for AppError {}
//! fn handle_error(error: ResilienceError<AppError>) {
//!     match error {
//!         ResilienceError::Timeout { layer } => {
//!             eprintln!("Timeout in {}", layer);
//!         }
//!         ResilienceError::CircuitOpen { name } => {
//!             eprintln!("Circuit breaker {:?} is open", name);
//!         }
//!         ResilienceError::BulkheadFull { concurrent_calls, max_concurrent } => {
//!             eprintln!("Bulkhead full: {}/{}", concurrent_calls, max_concurrent);
//!         }
//!         ResilienceError::RateLimited { retry_after } => {
//!             eprintln!("Rate limited, retry after {:?}", retry_after);
//!         }
//!         ResilienceError::InstanceEjected { name } => {
//!             eprintln!("Instance '{}' ejected by outlier detection", name);
//!         }
//!         ResilienceError::Application(app_err) => {
//!             eprintln!("Application error: {}", app_err);
//!         }
//!     }
//! }
//! ```
//!
//! # Helper Methods
//!
//! ```rust
//! use tower_resilience_core::ResilienceError;
//!
//! # #[derive(Debug)]
//! # struct AppError;
//! # impl std::fmt::Display for AppError {
//! #     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { Ok(()) }
//! # }
//! # impl std::error::Error for AppError {}
//! # let error: ResilienceError<AppError> = ResilienceError::Timeout { layer: "test" };
//! if error.is_timeout() {
//!     // Handle timeout from any layer
//! } else if error.is_application() {
//!     let app_error = error.application_error().unwrap();
//!     // Handle application-specific error
//! }
//! ```
//!
//! # When to Use
//!
//! **Use `ResilienceError<E>` when:**
//! - Building new services with multiple resilience layers
//! - You want zero boilerplate error handling
//! - Standard error categorization is sufficient
//! - You're prototyping or want to move fast
//!
//! **Use manual `From` implementations when:**
//! - You need very specific error semantics
//! - Different layers require different recovery strategies
//! - Integrating with legacy error types
//! - You need specialized error logging per layer
//!
//! # Migration
//!
//! Existing code using manual `From` implementations continues to work.
//! New code can adopt `ResilienceError<E>` incrementally:
//!
//! ```rust,ignore
//! // Old code (still works)
//! type ServiceError = MyCustomError; // with manual From impls
//!
//! // New code (zero boilerplate)
//! type ServiceError = ResilienceError<MyAppError>;
//! ```

use std::fmt;
use std::time::Duration;

/// A common error type that wraps all resilience layer errors.
///
/// This allows users to compose multiple resilience patterns without
/// writing any error conversion code. Each resilience layer error automatically
/// converts into the appropriate `ResilienceError` variant.
///
/// # Type Parameters
///
/// - `E`: The application-specific error type from the wrapped service
///
/// # Examples
///
/// ```
/// use tower_resilience_core::ResilienceError;
/// use std::time::Duration;
///
/// // Your application error
/// #[derive(Debug)]
/// enum AppError {
///     Network(String),
///     InvalidData,
/// }
///
/// impl std::fmt::Display for AppError {
///     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
///         match self {
///             AppError::Network(msg) => write!(f, "Network: {}", msg),
///             AppError::InvalidData => write!(f, "Invalid data"),
///         }
///     }
/// }
///
/// impl std::error::Error for AppError {}
///
/// // Use ResilienceError<AppError> throughout your resilience stack
/// type ServiceError = ResilienceError<AppError>;
///
/// // No From implementations needed - just use the error type!
/// fn handle_error(err: ServiceError) {
///     match err {
///         ResilienceError::Timeout { layer } => {
///             println!("Timeout in {}", layer);
///         }
///         ResilienceError::CircuitOpen { .. } => {
///             println!("Circuit breaker is open");
///         }
///         ResilienceError::Application(app_err) => {
///             println!("Application error: {}", app_err);
///         }
///         _ => println!("Other resilience error"),
///     }
/// }
/// ```
#[derive(Debug, Clone)]
pub enum ResilienceError<E> {
    /// A timeout occurred (from TimeLimiter or Bulkhead).
    Timeout {
        /// The layer that timed out (e.g., "time_limiter", "bulkhead")
        layer: &'static str,
    },

    /// Circuit breaker is open, call rejected.
    CircuitOpen {
        /// Circuit breaker name (if configured)
        name: Option<String>,
    },

    /// Bulkhead is at capacity, call rejected.
    BulkheadFull {
        /// Current number of concurrent calls
        concurrent_calls: usize,
        /// Maximum allowed concurrent calls
        max_concurrent: usize,
    },

    /// Rate limiter rejected the call.
    RateLimited {
        /// How long to wait before retrying (if available)
        retry_after: Option<Duration>,
    },

    /// An instance was ejected by outlier detection.
    InstanceEjected {
        /// The name of the ejected instance
        name: String,
    },

    /// The underlying application service returned an error.
    Application(E),
}

impl<E> fmt::Display for ResilienceError<E>
where
    E: fmt::Display,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ResilienceError::Timeout { layer } => write!(f, "Timeout in {}", layer),
            ResilienceError::CircuitOpen { name } => match name {
                Some(n) => write!(f, "Circuit breaker '{}' is open", n),
                None => write!(f, "Circuit breaker is open"),
            },
            ResilienceError::BulkheadFull {
                concurrent_calls,
                max_concurrent,
            } => write!(f, "Bulkhead full ({}/{})", concurrent_calls, max_concurrent),
            ResilienceError::RateLimited { retry_after } => match retry_after {
                Some(d) => write!(f, "Rate limited, retry after {:?}", d),
                None => write!(f, "Rate limited"),
            },
            ResilienceError::InstanceEjected { name } => {
                write!(f, "Instance '{}' ejected by outlier detection", name)
            }
            ResilienceError::Application(e) => write!(f, "Application error: {}", e),
        }
    }
}

impl<E> std::error::Error for ResilienceError<E> where E: std::error::Error {}

// Note: From implementations for each resilience layer error are provided
// by the individual crates (bulkhead, circuitbreaker, etc.) to avoid
// circular dependencies.

/// Trait for converting service errors into [`ResilienceError<E>`].
///
/// This is used by [`ResilienceErrorLayer`](crate::error_layer::ResilienceErrorLayer)
/// to convert each layer's error type into the unified `ResilienceError<E>`.
///
/// A blanket implementation is provided for any type that implements
/// `Into<ResilienceError<E>>`, which covers all tower-resilience service error types.
///
/// You typically don't need to implement this trait directly.
pub trait IntoResilienceError<E> {
    /// Convert this error into a `ResilienceError<E>`.
    fn into_resilience_error(self) -> ResilienceError<E>;
}

/// Blanket implementation: any type with `Into<ResilienceError<E>>` gets this for free.
///
/// This covers:
/// - `ResilienceError<E>` (identity, via `From<T> for T`)
/// - All service error types like `BulkheadServiceError<E>`, `CircuitBreakerError<E>`, etc.
impl<T, E> IntoResilienceError<E> for T
where
    T: Into<ResilienceError<E>>,
{
    fn into_resilience_error(self) -> ResilienceError<E> {
        self.into()
    }
}

impl<E> ResilienceError<E> {
    /// Returns `true` if this is a timeout error.
    pub fn is_timeout(&self) -> bool {
        matches!(self, ResilienceError::Timeout { .. })
    }

    /// Returns `true` if this is a circuit breaker error.
    pub fn is_circuit_open(&self) -> bool {
        matches!(self, ResilienceError::CircuitOpen { .. })
    }

    /// Returns `true` if this is a bulkhead error.
    pub fn is_bulkhead_full(&self) -> bool {
        matches!(self, ResilienceError::BulkheadFull { .. })
    }

    /// Returns `true` if this is a rate limiter error.
    pub fn is_rate_limited(&self) -> bool {
        matches!(self, ResilienceError::RateLimited { .. })
    }

    /// Returns `true` if this is an instance ejection error from outlier detection.
    pub fn is_instance_ejected(&self) -> bool {
        matches!(self, ResilienceError::InstanceEjected { .. })
    }

    /// Returns `true` if this is an application error.
    pub fn is_application(&self) -> bool {
        matches!(self, ResilienceError::Application(_))
    }

    /// Extracts the application error, if this is an `Application` variant.
    pub fn application_error(self) -> Option<E> {
        match self {
            ResilienceError::Application(e) => Some(e),
            _ => None,
        }
    }

    /// Maps the application error using a function.
    ///
    /// # Examples
    ///
    /// ```
    /// use tower_resilience_core::ResilienceError;
    ///
    /// let err: ResilienceError<String> = ResilienceError::Application("error".to_string());
    /// let mapped: ResilienceError<usize> = err.map_application(|s| s.len());
    /// assert_eq!(mapped.application_error(), Some(5));
    /// ```
    pub fn map_application<F, T>(self, f: F) -> ResilienceError<T>
    where
        F: FnOnce(E) -> T,
    {
        match self {
            ResilienceError::Timeout { layer } => ResilienceError::Timeout { layer },
            ResilienceError::CircuitOpen { name } => ResilienceError::CircuitOpen { name },
            ResilienceError::BulkheadFull {
                concurrent_calls,
                max_concurrent,
            } => ResilienceError::BulkheadFull {
                concurrent_calls,
                max_concurrent,
            },
            ResilienceError::RateLimited { retry_after } => {
                ResilienceError::RateLimited { retry_after }
            }
            ResilienceError::InstanceEjected { name } => ResilienceError::InstanceEjected { name },
            ResilienceError::Application(e) => ResilienceError::Application(f(e)),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[derive(Debug, Clone)]
    struct TestError;

    impl fmt::Display for TestError {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            write!(f, "test error")
        }
    }

    impl std::error::Error for TestError {}

    /// Compile-time assertion that ResilienceError is Send + Sync + 'static
    /// when the inner error type is Send + Sync + 'static.
    /// This is required for compatibility with tower's BoxError.
    const _: () = {
        const fn assert_send_sync_static<T: Send + Sync + 'static>() {}
        assert_send_sync_static::<ResilienceError<TestError>>();
    };

    #[test]
    fn test_into_box_error() {
        let err: ResilienceError<TestError> = ResilienceError::Timeout { layer: "test" };
        let boxed: Box<dyn std::error::Error + Send + Sync> = Box::new(err);
        assert!(boxed.to_string().contains("Timeout"));
    }

    #[test]
    fn test_application_error_into_box_error() {
        let err: ResilienceError<TestError> = ResilienceError::Application(TestError);
        let boxed: Box<dyn std::error::Error + Send + Sync> = Box::new(err);
        assert!(boxed.to_string().contains("test error"));
    }
}