deadpool/managed/
errors.rs

1use std::fmt;
2
3use super::hooks::HookError;
4
5/// Possible errors returned by the [`Manager::recycle()`] method.
6///
7/// [`Manager::recycle()`]: super::Manager::recycle
8#[derive(Debug)]
9pub enum RecycleError<E> {
10    /// Recycling failed for some other reason.
11    Message(String),
12
13    /// Recycling failed for some other reason.
14    StaticMessage(&'static str),
15
16    /// Error caused by the backend.
17    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/// Possible steps causing the timeout in an error returned by [`Pool::get()`]
49/// method.
50///
51/// [`Pool::get()`]: super::Pool::get
52#[derive(Clone, Copy, Debug)]
53pub enum TimeoutType {
54    /// Timeout happened while waiting for a slot to become available.
55    Wait,
56
57    /// Timeout happened while creating a new object.
58    Create,
59
60    /// Timeout happened while recycling an object.
61    Recycle,
62}
63
64/// Possible errors returned by [`Pool::get()`] method.
65///
66/// [`Pool::get()`]: super::Pool::get
67#[derive(Debug)]
68pub enum PoolError<E> {
69    /// Timeout happened.
70    Timeout(TimeoutType),
71
72    /// Backend reported an error.
73    Backend(E),
74
75    /// [`Pool`] has been closed.
76    ///
77    /// [`Pool`]: super::Pool
78    Closed,
79
80    /// No [`Runtime`] was specified.
81    ///
82    /// [`Runtime`]: crate::Runtime
83    NoRuntimeSpecified,
84
85    /// A `post_create` hook reported an error.
86    PostCreateHook(HookError<E>),
87
88    /// A `pre_recycle` hook reported an error.
89    PreRecycleHook(HookError<E>),
90
91    /// A `post_recycle` hook reported an error.
92    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}