#[derive(Debug, thiserror::Error)]
pub enum DataSourceError {
#[error("Connection failed: {0}")]
ConnectionFailed(String),
#[error("Query execution failed: {0}")]
QueryFailed(String),
#[error("Schema introspection failed: {0}")]
SchemaFailed(String),
#[error("Unsupported operation: {0}")]
UnsupportedOperation(String),
#[error("Authentication failed: {0}")]
AuthenticationFailed(String),
#[error("Invalid configuration: {0}")]
InvalidConfiguration(String),
#[error("Timeout error: {0}")]
Timeout(String),
#[error("Data type conversion failed: {0}")]
DataTypeConversion(String),
#[error("Transaction failed: {0}")]
TransactionFailed(String),
#[error("Connection pool exhausted: {0}")]
PoolExhausted(String),
}
impl DataSourceError {
pub fn connection_failed(message: impl Into<String>) -> Self {
Self::ConnectionFailed(message.into())
}
pub fn query_failed(message: impl Into<String>) -> Self {
Self::QueryFailed(message.into())
}
pub fn schema_failed(message: impl Into<String>) -> Self {
Self::SchemaFailed(message.into())
}
pub fn unsupported_operation(message: impl Into<String>) -> Self {
Self::UnsupportedOperation(message.into())
}
pub fn authentication_failed(message: impl Into<String>) -> Self {
Self::AuthenticationFailed(message.into())
}
pub fn invalid_configuration(message: impl Into<String>) -> Self {
Self::InvalidConfiguration(message.into())
}
pub fn timeout(message: impl Into<String>) -> Self {
Self::Timeout(message.into())
}
pub fn data_type_conversion(message: impl Into<String>) -> Self {
Self::DataTypeConversion(message.into())
}
pub fn transaction_failed(message: impl Into<String>) -> Self {
Self::TransactionFailed(message.into())
}
pub fn pool_exhausted(message: impl Into<String>) -> Self {
Self::PoolExhausted(message.into())
}
pub fn is_connection_error(&self) -> bool {
matches!(
self,
DataSourceError::ConnectionFailed(_) | DataSourceError::AuthenticationFailed(_)
)
}
pub fn is_query_error(&self) -> bool {
matches!(
self,
DataSourceError::QueryFailed(_) | DataSourceError::UnsupportedOperation(_)
)
}
pub fn is_configuration_error(&self) -> bool {
matches!(self, DataSourceError::InvalidConfiguration(_))
}
pub fn is_timeout_error(&self) -> bool {
matches!(self, DataSourceError::Timeout(_))
}
pub fn is_transaction_error(&self) -> bool {
matches!(self, DataSourceError::TransactionFailed(_))
}
pub fn is_pool_error(&self) -> bool {
matches!(self, DataSourceError::PoolExhausted(_))
}
}