Files
student-management-system/dao/exceptions.py
T
2026-09-18 16:53:21 +08:00

27 lines
771 B
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# dao/exceptions.py
# 数据访问层异常:不依赖 FastAPI,由 main.py 统一注册处理器映射成 HTTP 状态码
#
# 这样 api 层就不需要 try/except,也不需要 import HTTPException,
# 只负责「调用 dao + 组装响应」两件事。
class DaoError(Exception):
"""DAO 层异常基类:携带 HTTP 状态码与前端可读的提示信息"""
status_code = 500
def __init__(self, detail: str):
self.detail = detail
super().__init__(detail)
class NotFoundError(DaoError):
"""目标记录不存在、已删除,或外键指向的记录不存在 → 404"""
status_code = 404
class ConflictError(DaoError):
"""主键重复、唯一字段冲突、一对一关系重复 → 409"""
status_code = 409