Skip to main content

sz_orm_sqlx/
error.rs

1//! sqlx 错误到 sz-orm-core DbError 的映射
2
3use sz_orm_core::{DbError, PoolError};
4
5/// 将 sqlx::Error 转换为 DbError
6pub fn map_sqlx_error(e: sqlx::Error) -> DbError {
7    match e {
8        sqlx::Error::Database(db_err) => {
9            // 用 code() 和 message() 判断错误类型
10            let msg = db_err.message().to_string();
11            let code = db_err.code().map(|c| c.into_owned()).unwrap_or_default();
12            // PostgreSQL SQLSTATE codes
13            if code == "23505"
14                || msg.contains("Duplicate entry")
15                || msg.contains("unique constraint")
16            {
17                // 优先映射到细粒度的 UniqueViolation(唯一约束),保留 AlreadyExists 向后兼容
18                DbError::UniqueViolation(msg)
19            } else if code == "23503" || msg.contains("foreign key constraint") {
20                DbError::ForeignKeyViolation(msg)
21            } else if code.starts_with("23") || msg.contains("constraint") {
22                DbError::ConstraintViolation(msg)
23            } else if code.starts_with("42") || msg.contains("syntax") {
24                DbError::InvalidInput(msg)
25            } else {
26                DbError::QueryError(msg)
27            }
28        }
29        sqlx::Error::PoolClosed => {
30            DbError::PoolError(PoolError::Internal("sqlx pool closed".to_string()))
31        }
32        sqlx::Error::PoolTimedOut => DbError::PoolError(PoolError::Timeout),
33        sqlx::Error::Io(io) => DbError::IoError(io.to_string()),
34        sqlx::Error::Tls(tls) => DbError::ConnectionError(tls.to_string()),
35        sqlx::Error::Protocol(p) => DbError::ConnectionError(p.to_string()),
36        sqlx::Error::RowNotFound => DbError::NotFound("row not found".to_string()),
37        other => DbError::Internal(other.to_string()),
38    }
39}