Skip to main content

reinhardt_db/pool/
errors.rs

1//! Error types for connection pooling
2
3use thiserror::Error;
4
5#[non_exhaustive]
6#[derive(Error, Debug)]
7/// Defines possible pool error values.
8pub enum PoolError {
9	#[error("Pool is closed")]
10	/// PoolClosed variant.
11	PoolClosed,
12
13	#[error("Connection timeout")]
14	/// Timeout variant.
15	Timeout,
16
17	#[error("Pool exhausted (max connections reached)")]
18	/// PoolExhausted variant.
19	PoolExhausted,
20
21	#[error("Invalid connection")]
22	/// InvalidConnection variant.
23	InvalidConnection,
24
25	#[error("Database error: {0}")]
26	/// Database variant.
27	Database(#[from] sqlx::Error),
28
29	#[error("Configuration error: {0}")]
30	/// Config variant.
31	Config(String),
32
33	#[error("Connection error: {0}")]
34	/// Connection variant.
35	Connection(String),
36
37	#[error("Pool not found: {0}")]
38	/// PoolNotFound variant.
39	PoolNotFound(String),
40}
41
42/// Type alias for pool result.
43pub type PoolResult<T> = Result<T, PoolError>;
44
45#[cfg(test)]
46mod tests {
47	use super::*;
48
49	#[test]
50	fn display_preserves_every_pool_error_message() {
51		let cases = [
52			(PoolError::PoolClosed, "Pool is closed"),
53			(PoolError::Timeout, "Connection timeout"),
54			(
55				PoolError::PoolExhausted,
56				"Pool exhausted (max connections reached)",
57			),
58			(PoolError::InvalidConnection, "Invalid connection"),
59			(
60				PoolError::Config("max must be positive".to_string()),
61				"Configuration error: max must be positive",
62			),
63			(
64				PoolError::Connection("refused".to_string()),
65				"Connection error: refused",
66			),
67			(
68				PoolError::PoolNotFound("analytics".to_string()),
69				"Pool not found: analytics",
70			),
71		];
72
73		for (error, expected) in cases {
74			assert_eq!(error.to_string(), expected);
75		}
76	}
77
78	#[test]
79	fn sqlx_pool_closed_error_is_preserved_as_database_error() {
80		let error = PoolError::from(sqlx::Error::PoolClosed);
81
82		assert!(matches!(
83			error,
84			PoolError::Database(sqlx::Error::PoolClosed)
85		));
86	}
87}