diff --git a/backend/app/common/exception/exception_handler.py b/backend/app/common/exception/exception_handler.py index d818b4d8..c1a5b337 100644 --- a/backend/app/common/exception/exception_handler.py +++ b/backend/app/common/exception/exception_handler.py @@ -92,6 +92,24 @@ def register_exception(app: FastAPI): ), ) + @app.exception_handler(AssertionError) + async def assertion_error_handler(request: Request, exc: AssertionError): + """ + 断言错误处理 + :param request: + :param exc: + :return: + """ + return JSONResponse( + status_code=StandardResponseCode.HTTP_500, + content=ResponseModel( + code=StandardResponseCode.HTTP_500, + msg=str(''.join(exc.args) if exc.args else exc.__doc__), + ).model_dump() + if settings.ENVIRONMENT == 'dev' + else await response_base.fail(CustomResponseCode.HTTP_500), + ) + @app.exception_handler(Exception) async def all_exception_handler(request: Request, exc: Exception): """ @@ -111,24 +129,6 @@ def register_exception(app: FastAPI): ).dict(), background=exc.background, ) - - elif isinstance(exc, AssertionError): - if exc.args: - msg = ','.join(exc.args) - else: - msg = exc.__repr__() - if not exc.__repr__().startswith('AssertionError'): - msg = exc.__doc__ - return JSONResponse( - status_code=StandardResponseCode.HTTP_500, - content=ResponseModel( - code=StandardResponseCode.HTTP_500, - msg=str(msg), - ).dict() - if settings.ENVIRONMENT == 'dev' - else await response_base.fail(res=CustomResponseCode.HTTP_500), - ) - else: import traceback diff --git a/backend/app/crud/base.py b/backend/app/crud/base.py index cff5b5f7..7d2859d8 100644 --- a/backend/app/crud/base.py +++ b/backend/app/crud/base.py @@ -36,8 +36,8 @@ class CRUDBase(Generic[ModelType, CreateSchemaType, UpdateSchemaType]): :param del_flag: :return: """ - assert pk is not None or name is not None, '查询错误, pk 和 name 参数不能同时存在' - assert pk is None or name is None, '查询错误, pk 和 name 参数不能同时为空' + assert pk is not None or name is not None, '查询错误, pk 和 name 参数不能同时为空' + assert pk is None or name is None, '查询错误, pk 和 name 参数不能同时存在' where_list = [self.model.id == pk] if pk is not None else [self.model.name == name] if status is not None: assert status in (0, 1), '查询错误, status 参数只能为 0 或 1'