deadpool/managed/
errors.rs
1use std::fmt;
2
3use super::hooks::HookError;
4
5#[derive(Debug)]
9pub enum RecycleError<E> {
10 Message(String),
12
13 StaticMessage(&'static str),
15
16 Backend(E),
18}
19
20impl<E> From<E> for RecycleError<E> {
21 fn from(e: E) -> Self {
22 Self::Backend(e)
23 }
24}
25
26impl<E: fmt::Display> fmt::Display for RecycleError<E> {
27 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28 match self {
29 Self::Message(msg) => write!(f, "Error occurred while recycling an object: {}", msg),
30 Self::StaticMessage(msg) => {
31 write!(f, "Error occurred while recycling an object: {}", msg)
32 }
33 Self::Backend(e) => write!(f, "Error occurred while recycling an object: {}", e),
34 }
35 }
36}
37
38impl<E: std::error::Error + 'static> std::error::Error for RecycleError<E> {
39 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
40 match self {
41 Self::Message(_) => None,
42 Self::StaticMessage(_) => None,
43 Self::Backend(e) => Some(e),
44 }
45 }
46}
47
48#[derive(Clone, Copy, Debug)]
53pub enum TimeoutType {
54 Wait,
56
57 Create,
59
60 Recycle,
62}
63
64#[derive(Debug)]
68pub enum PoolError<E> {
69 Timeout(TimeoutType),
71
72 Backend(E),
74
75 Closed,
79
80 NoRuntimeSpecified,
84
85 PostCreateHook(HookError<E>),
87
88 PreRecycleHook(HookError<E>),
90
91 PostRecycleHook(HookError<E>),
93}
94
95impl<E> From<E> for PoolError<E> {
96 fn from(e: E) -> Self {
97 Self::Backend(e)
98 }
99}
100
101impl<E: fmt::Display> fmt::Display for PoolError<E> {
102 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
103 match self {
104 Self::Timeout(tt) => match tt {
105 TimeoutType::Wait => write!(
106 f,
107 "Timeout occurred while waiting for a slot to become available"
108 ),
109 TimeoutType::Create => write!(f, "Timeout occurred while creating a new object"),
110 TimeoutType::Recycle => write!(f, "Timeout occurred while recycling an object"),
111 },
112 Self::Backend(e) => write!(f, "Error occurred while creating a new object: {}", e),
113 Self::Closed => write!(f, "Pool has been closed"),
114 Self::NoRuntimeSpecified => write!(f, "No runtime specified"),
115 Self::PostCreateHook(e) => writeln!(f, "`post_create` hook failed: {}", e),
116 Self::PreRecycleHook(e) => writeln!(f, "`pre_recycle` hook failed: {}", e),
117 Self::PostRecycleHook(e) => writeln!(f, "`post_recycle` hook failed: {}", e),
118 }
119 }
120}
121
122impl<E: std::error::Error + 'static> std::error::Error for PoolError<E> {
123 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
124 match self {
125 Self::Timeout(_) | Self::Closed | Self::NoRuntimeSpecified => None,
126 Self::Backend(e) => Some(e),
127 Self::PostCreateHook(e) | Self::PreRecycleHook(e) | Self::PostRecycleHook(e) => Some(e),
128 }
129 }
130}