Skip to main content

reinhardt_db/backends/
error.rs

1//! Error types for database operations
2
3use thiserror::Error;
4
5/// Database operation errors
6#[derive(Error, Debug)]
7pub enum DatabaseError {
8	/// Feature not supported by this database
9	#[error("Feature '{feature}' is not supported by {database}")]
10	UnsupportedFeature { database: String, feature: String },
11
12	/// Operation not supported by this backend
13	#[error("Not supported: {0}")]
14	NotSupported(String),
15
16	/// SQL syntax error
17	#[error("SQL syntax error: {0}")]
18	SyntaxError(String),
19
20	/// Type conversion error
21	#[error("Type conversion error: {0}")]
22	TypeError(String),
23
24	/// Connection error
25	#[error("Connection error: {0}")]
26	ConnectionError(String),
27
28	/// Query execution error
29	#[error("Query execution error: {0}")]
30	QueryError(String),
31
32	/// Serialization/deserialization error
33	#[error("Serialization error: {0}")]
34	SerializationError(String),
35
36	/// Configuration error
37	#[error("Configuration error: {0}")]
38	ConfigError(String),
39
40	/// Column not found error
41	#[error("Column not found: {0}")]
42	ColumnNotFound(String),
43
44	/// Transaction error
45	#[error("Transaction error: {0}")]
46	TransactionError(String),
47
48	/// Generic database error
49	#[error("Database error: {0}")]
50	Other(String),
51}
52
53/// Result type for database operations
54pub type Result<T> = std::result::Result<T, DatabaseError>;
55
56impl From<serde_json::Error> for DatabaseError {
57	fn from(err: serde_json::Error) -> Self {
58		DatabaseError::SerializationError(err.to_string())
59	}
60}
61
62impl From<sqlx::Error> for DatabaseError {
63	fn from(err: sqlx::Error) -> Self {
64		use sqlx::Error::*;
65		match err {
66			Configuration(msg) => DatabaseError::ConfigError(msg.to_string()),
67			Database(e) => DatabaseError::QueryError(e.to_string()),
68			Io(e) => DatabaseError::ConnectionError(e.to_string()),
69			Tls(e) => DatabaseError::ConnectionError(e.to_string()),
70			Protocol(msg) => DatabaseError::QueryError(msg),
71			RowNotFound => DatabaseError::QueryError("Row not found".to_string()),
72			TypeNotFound { type_name } => {
73				DatabaseError::TypeError(format!("Type not found: {}", type_name))
74			}
75			ColumnIndexOutOfBounds { index, len } => DatabaseError::QueryError(format!(
76				"Column index {} out of bounds (len: {})",
77				index, len
78			)),
79			ColumnNotFound(name) => {
80				DatabaseError::QueryError(format!("Column not found: {}", name))
81			}
82			ColumnDecode { index, source } => {
83				DatabaseError::TypeError(format!("Failed to decode column {}: {}", index, source))
84			}
85			Decode(e) => DatabaseError::TypeError(e.to_string()),
86			PoolTimedOut => DatabaseError::ConnectionError("Pool timed out".to_string()),
87			PoolClosed => DatabaseError::ConnectionError("Pool closed".to_string()),
88			WorkerCrashed => DatabaseError::ConnectionError("Worker crashed".to_string()),
89			Migrate(e) => DatabaseError::QueryError(format!("Migration error: {}", e)),
90			_ => DatabaseError::Other(err.to_string()),
91		}
92	}
93}