refactor(core): 1062 唯一键冲突判定收口至 core/db_errors
六个 service 各自复制的 _is_duplicate_entry(口径已漂移:rating/product 用 _ERR_DUP_ENTRY 常量、其余四处写字面量 1062)收敛为共享函数; is_duplicate_entry 语义与文案注释各处保留,非 1062 完整性错误仍原样 上抛由全局处理器收敛。 Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
1 parent
5a3778a424
commit
a0086554b1
7 files changed
+31
-19
No files matched your search
@@ -26,6 +26,7 @@ from app.core.constants import (
|
||||
CUSTOMER_TYPES,
|
||||
MODULE_CUSTOMER,
|
||||
)
|
||||
from app.core.db_errors import is_duplicate_entry
|
||||
from app.core.exceptions import BadRequestError, NotFoundError
|
||||
from app.core.response import PageQuery, PageResult
|
||||
from app.models import Customer, OwnershipChangeLog, Personnel, User
|
||||
@@ -108,14 +109,13 @@ def _ensure_owner_personnel(session: Session, personnel_id: int) -> Personnel:
|
||||
|
||||
|
||||
def _is_duplicate_entry(exc: IntegrityError) -> bool:
|
||||
"""判断 IntegrityError 是否为 MySQL 唯一键冲突(错误码 1062)。
|
||||
"""判断 IntegrityError 是否为 MySQL 唯一键冲突(1062)。
|
||||
|
||||
customers 上还有归属人员外键(RESTRICT),并发窗口内删除人员等场景
|
||||
会抛 1452 —— 不能一并翻译成「已存在」,否则用户会被错误提示带偏;
|
||||
非 1062 原样上抛,由全局处理器按 500 收敛。
|
||||
"""
|
||||
args = getattr(exc.orig, "args", ())
|
||||
return bool(args) and args[0] == 1062
|
||||
return is_duplicate_entry(exc)
|
||||
|
||||
|
||||
def list_customers(session: Session, query: CustomerQuery) -> PageResult[CustomerOut]:
|
||||
|
||||
Reference in new issue
Block a user