#[cfg(all(feature = "sea-orm-internal", feature = "sqlx-dep"))]
pub use sqlx::error::Error as SqlxError;
#[cfg(all(feature = "sea-orm-internal", feature = "sqlx-mysql"))]
pub use sqlx::mysql::MySqlDatabaseError as SqlxMySqlError;
#[cfg(all(feature = "sea-orm-internal", feature = "sqlx-postgres"))]
pub use sqlx::postgres::PgDatabaseError as SqlxPostgresError;
#[cfg(all(feature = "sea-orm-internal", feature = "sqlx-sqlite"))]
pub use sqlx::sqlite::SqliteError as SqlxSqliteError;
use thiserror::Error;
#[derive(Error, Debug)]
pub enum DbErr {
#[error("Failed to acquire connection from pool: {0}")]
ConnectionAcquire(ConnAcquireErr),
#[error("Error converting `{from}` into `{into}`: {source}")]
TryIntoErr {
from: &'static str,
into: &'static str,
source: Box<dyn std::error::Error + Send + Sync>,
},
#[error("Connection Error: {0}")]
Conn(#[source] RuntimeErr),
#[error("Execution Error: {0}")]
Exec(#[source] RuntimeErr),
#[error("Query Error: {0}")]
Query(#[source] RuntimeErr),
#[error("Type '{0}' cannot be converted from u64")]
ConvertFromU64(&'static str),
#[error("Failed to unpack last_insert_id")]
UnpackInsertId,
#[error("Failed to get primary key from model")]
UpdateGetPrimaryKey,
#[error("RecordNotFound Error: {0}")]
RecordNotFound(String),
#[error("Attribute {0} is NotSet")]
AttrNotSet(String),
#[error("Custom Error: {0}")]
Custom(String),
#[error("Type Error: {0}")]
Type(String),
#[error("Json Error: {0}")]
Json(String),
#[error("Migration Error: {0}")]
Migration(String),
#[error("None of the records are inserted")]
RecordNotInserted,
#[error("None of the records are updated")]
RecordNotUpdated,
}
#[derive(Error, Debug, PartialEq, Eq)]
pub enum ConnAcquireErr {
#[error("Connection pool timed out")]
Timeout,
#[error("Connection closed")]
ConnectionClosed,
}
#[derive(Error, Debug)]
pub enum RuntimeErr {
#[cfg(feature = "sqlx-dep")]
#[error("{0}")]
SqlxError(sqlx::error::Error),
#[error("{0}")]
Internal(String),
}
impl PartialEq for DbErr {
fn eq(&self, other: &Self) -> bool {
self.to_string() == other.to_string()
}
}
impl Eq for DbErr {}
#[derive(Error, Debug)]
#[error("Failed to match \"{0}\" as Column")]
pub struct ColumnFromStrErr(pub String);
#[allow(dead_code)]
pub(crate) fn conn_err<T>(s: T) -> DbErr
where
T: ToString,
{
DbErr::Conn(RuntimeErr::Internal(s.to_string()))
}
#[allow(dead_code)]
pub(crate) fn exec_err<T>(s: T) -> DbErr
where
T: ToString,
{
DbErr::Exec(RuntimeErr::Internal(s.to_string()))
}
#[allow(dead_code)]
pub(crate) fn query_err<T>(s: T) -> DbErr
where
T: ToString,
{
DbErr::Query(RuntimeErr::Internal(s.to_string()))
}
#[allow(dead_code)]
pub(crate) fn type_err<T>(s: T) -> DbErr
where
T: ToString,
{
DbErr::Type(s.to_string())
}
#[allow(dead_code)]
pub(crate) fn json_err<T>(s: T) -> DbErr
where
T: ToString,
{
DbErr::Json(s.to_string())
}
#[allow(dead_code)]
#[cfg(feature = "sqlx-dep")]
pub(crate) fn conn_acquire_err(sqlx_err: sqlx::Error) -> DbErr {
match sqlx_err {
sqlx::Error::PoolTimedOut => DbErr::ConnectionAcquire(ConnAcquireErr::Timeout),
sqlx::Error::PoolClosed => DbErr::ConnectionAcquire(ConnAcquireErr::ConnectionClosed),
_ => DbErr::Conn(RuntimeErr::SqlxError(sqlx_err)),
}
}
#[derive(Error, Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum SqlErr {
#[error("Unique Constraint Violated: {0}")]
UniqueConstraintViolation(String),
#[error("Foreign Key Constraint Violated: {0}")]
ForeignKeyConstraintViolation(String),
}
#[allow(dead_code)]
impl DbErr {
pub fn sql_err(&self) -> Option<SqlErr> {
#[cfg(any(
feature = "sqlx-mysql",
feature = "sqlx-postgres",
feature = "sqlx-sqlite"
))]
{
use std::ops::Deref;
if let DbErr::Exec(RuntimeErr::SqlxError(sqlx::Error::Database(e)))
| DbErr::Query(RuntimeErr::SqlxError(sqlx::Error::Database(e))) = self
{
let error_code = e.code().unwrap_or_default();
let _error_code_expanded = error_code.deref();
#[cfg(feature = "sqlx-mysql")]
if e.try_downcast_ref::<sqlx::mysql::MySqlDatabaseError>()
.is_some()
{
let error_number = e
.try_downcast_ref::<sqlx::mysql::MySqlDatabaseError>()?
.number();
match error_number {
1022 | 1062 | 1169 | 1586 => {
return Some(SqlErr::UniqueConstraintViolation(e.message().into()))
}
1216 | 1217 | 1451 | 1452 | 1557 | 1761 | 1762 => {
return Some(SqlErr::ForeignKeyConstraintViolation(e.message().into()))
}
_ => return None,
}
}
#[cfg(feature = "sqlx-postgres")]
if e.try_downcast_ref::<sqlx::postgres::PgDatabaseError>()
.is_some()
{
match _error_code_expanded {
"23505" => {
return Some(SqlErr::UniqueConstraintViolation(e.message().into()))
}
"23503" => {
return Some(SqlErr::ForeignKeyConstraintViolation(e.message().into()))
}
_ => return None,
}
}
#[cfg(feature = "sqlx-sqlite")]
if e.try_downcast_ref::<sqlx::sqlite::SqliteError>().is_some() {
match _error_code_expanded {
"1555" | "2067" => {
return Some(SqlErr::UniqueConstraintViolation(e.message().into()))
}
"787" => {
return Some(SqlErr::ForeignKeyConstraintViolation(e.message().into()))
}
_ => return None,
}
}
}
}
None
}
}