use std::fmt;
use arcature_db::sea_orm;
use arcature_db::sqlx;
#[derive(Debug)]
pub enum DataError {
Database(sea_orm::DbErr),
Sqlx(sqlx::Error),
Pagination(PaginationError),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PaginationError {
PageMustBePositive,
PerPageMustBePositive,
}
impl DataError {
#[must_use]
pub fn id(&self) -> &'static str {
match self {
Self::Database(_) => "arcature_data.database",
Self::Sqlx(_) => "arcature_data.sqlx",
Self::Pagination(PaginationError::PageMustBePositive) => {
"arcature_data.pagination.page_must_be_positive"
}
Self::Pagination(PaginationError::PerPageMustBePositive) => {
"arcature_data.pagination.per_page_must_be_positive"
}
}
}
}
impl fmt::Display for DataError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Database(error) => write!(formatter, "database error: {error}"),
Self::Sqlx(error) => write!(formatter, "sqlx error: {error}"),
Self::Pagination(PaginationError::PageMustBePositive) => {
write!(formatter, "pagination page must be >= 1 (1-based)")
}
Self::Pagination(PaginationError::PerPageMustBePositive) => {
write!(formatter, "pagination per_page must be >= 1")
}
}
}
}
impl std::error::Error for DataError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Database(error) => Some(error),
Self::Sqlx(error) => Some(error),
Self::Pagination(_) => None,
}
}
}
impl From<sea_orm::DbErr> for DataError {
fn from(value: sea_orm::DbErr) -> Self {
Self::Database(value)
}
}
impl From<sqlx::Error> for DataError {
fn from(value: sqlx::Error) -> Self {
Self::Sqlx(value)
}
}
impl From<PaginationError> for DataError {
fn from(value: PaginationError) -> Self {
Self::Pagination(value)
}
}