use thiserror::Error;
#[derive(Debug, Error, PartialEq)]
pub enum QueryError {
#[error("Invalid email format: {0}")]
InvalidEmailFormat(String),
#[error("SQL syntax error: {0}")]
SyntaxError(String),
#[error("Database constraint violation: {0}")]
ConstraintViolation(String),
#[error("No results found for the given criteria")]
NoResults,
#[error("Data mapping error: {0}")]
MappingError(String),
#[error("Unexpected number of rows affected. Expected {expected}, got {actual}")]
AffectedRowsMismatch { expected: u64, actual: u64 },
#[error("Query execution failed: {0}")]
ExecutionFailed(String),
#[error("Invalid ID provided: {0}")]
InvalidId(String),
#[error("Invalid password provided for email: {0}")]
InvalidPassword(String),
#[error("Email not found: {0}")]
EmailNotFound(String),
}
impl From<tokio_postgres::Error> for QueryError {
fn from(err: tokio_postgres::Error) -> Self {
let err_str = err.to_string();
if err_str.contains("unique constraint") {
QueryError::ConstraintViolation(err_str)
} else if err_str.contains("0 rows") {
QueryError::NoResults
} else {
QueryError::ExecutionFailed(err_str)
}
}
}