circuitbreaker_rs/
error.rs1use std::error::Error;
4use std::fmt::{self, Display, Formatter};
5
6pub type BreakerResult<T, E> = Result<T, BreakerError<E>>;
8
9#[derive(Debug)]
11pub enum BreakerError<E> {
12 Open,
14
15 Operation(E),
17
18 Internal(InternalError),
20}
21
22#[derive(Debug)]
24pub enum InternalError {
25 StateTransition,
27
28 FailureTracking,
30
31 PolicyEvaluation,
33
34 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 {}