circuitbreaker_rs/
error.rs

1//! Error types for the circuit breaker library.
2
3use std::error::Error;
4use std::fmt::{self, Display, Formatter};
5
6/// Result type for circuit breaker operations.
7pub type BreakerResult<T, E> = Result<T, BreakerError<E>>;
8
9/// Error type for circuit breaker operations.
10#[derive(Debug)]
11pub enum BreakerError<E> {
12    /// The circuit is open, calls are not permitted.
13    Open,
14
15    /// The underlying operation failed.
16    Operation(E),
17
18    /// The circuit breaker encountered an internal error.
19    Internal(InternalError),
20}
21
22/// Internal errors that can occur within the circuit breaker.
23#[derive(Debug)]
24pub enum InternalError {
25    /// Failed to transition between states.
26    StateTransition,
27
28    /// Error in tracking failures.
29    FailureTracking,
30
31    /// Error in policy evaluation.
32    PolicyEvaluation,
33
34    /// Hook execution failed.
35    HookExecution,
36}
37
38impl<E> Display for BreakerError<E>
39where
40    E: Display,
41{
42    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
43        match self {
44            BreakerError::Open => write!(f, "Circuit breaker is open"),
45            BreakerError::Operation(e) => write!(f, "Operation error: {}", e),
46            BreakerError::Internal(e) => write!(f, "Circuit breaker internal error: {}", e),
47        }
48    }
49}
50
51impl Display for InternalError {
52    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
53        match self {
54            InternalError::StateTransition => write!(f, "Failed to transition between states"),
55            InternalError::FailureTracking => write!(f, "Error in tracking failures"),
56            InternalError::PolicyEvaluation => write!(f, "Error in policy evaluation"),
57            InternalError::HookExecution => write!(f, "Hook execution failed"),
58        }
59    }
60}
61
62impl<E: Error + 'static> Error for BreakerError<E> {
63    fn source(&self) -> Option<&(dyn Error + 'static)> {
64        match self {
65            BreakerError::Open => None,
66            BreakerError::Operation(e) => Some(e),
67            BreakerError::Internal(_) => None,
68        }
69    }
70}
71
72impl Error for InternalError {}