refactor: P4a 服务层改造地基(BizError + base_repository + base_service)
- src/core/exceptions.py:BizError/NotFoundError/PermissionDeniedError - 全局异常处理器接入 BizError → R.failed(含事务回滚语义) - src/common/base_repository.py:泛型 CRUD 仓储基类 - src/common/base_service.py:服务基类(入参数据/出参数据范式) 【验收】BizError 端到端转换为 R.failed(code=403/ok=false);应用启动 Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude
parent
d33dc1fc34
commit
a6e094378c
@@ -0,0 +1,44 @@
|
||||
"""
|
||||
业务异常定义
|
||||
=============================================================
|
||||
|
||||
业务代码通过 `raise BizError(...)` 表达业务失败,由全局异常处理器
|
||||
统一转换为 R.failed 响应。业务层不再 `return R.failed`,使服务函数
|
||||
保持「入参数据、出参数据」的纯函数形态(可单元测试)。
|
||||
|
||||
使用示例:
|
||||
from src.core.exceptions import BizError
|
||||
|
||||
def add_position(data):
|
||||
form = PositionForm(data=data)
|
||||
if not form.validate():
|
||||
raise BizError(regular.get_err(form))
|
||||
position.save()
|
||||
"""
|
||||
|
||||
from src.common.constants.common import MESSAGE_FAIL
|
||||
|
||||
|
||||
class BizError(Exception):
|
||||
"""业务异常:统一转为 R.failed 响应"""
|
||||
|
||||
def __init__(self, msg: str = MESSAGE_FAIL, code: int = 1, data=None, **kwargs):
|
||||
self.msg = msg
|
||||
self.code = code
|
||||
self.data = data
|
||||
self.kwargs = kwargs
|
||||
super().__init__(msg)
|
||||
|
||||
|
||||
class NotFoundError(BizError):
|
||||
"""资源不存在(默认提示)"""
|
||||
|
||||
def __init__(self, msg: str = "资源不存在", **kwargs):
|
||||
super().__init__(msg=msg, **kwargs)
|
||||
|
||||
|
||||
class PermissionDeniedError(BizError):
|
||||
"""权限不足"""
|
||||
|
||||
def __init__(self, msg: str = "权限不足", code: int = 403, **kwargs):
|
||||
super().__init__(msg=msg, code=code, **kwargs)
|
||||
Reference in New Issue
Block a user