kinit/kinit-api/core/exception.py
ktianc c65ff06918 版本升级:
1. 修复(kinit-api):utils/cache.py 日志模块导入问题修复
2. 修复(kinit-api):token解析失败会报错问题修复
3. 优化(kinit-api):用户登录认证失败返回值优化
4. 优化(kinit-api):获取redis方式统一改为redis_getter(request)
5. 优化(kini-api):文件IO修改为异步操作
6. 优化(kinit-api):关联创建人时将user_id改为create_user_id
7. 文档(kinit-api):kinit-api/README.md 加入查询数据文档
8. 修复(kinit-admin):用户无法导出问题修复
9. 优化(kinit-admin):角色新增与编辑框使用默认父子联动
10. 更新(kinit-admin):usePermissionStore 改为 useRouterStoreWithOut,因为此文件主要记录路由
11. 更新(kinit-admin):取消用户信息的持久化存储,改为仅保存在pinia store共享中,并添加roles,permissions信息
12. 修复(kinit-admin):菜单新增与编辑框,目录与菜单切换时会出现抖动问题修复
13. 优化(kinit-admin):src\hooks\web\useTable.ts 优化删除数据方法
14. 优化(kinit-admin):config/services.ts 新增返回403状态码时直接退出系统
15. 优化(kinit-admin):将store中的本文件使用store调用的,改为this
16. 更新(kinit-admin):路由拦截更新
17. 更新(kinit-api,kinit-admin,kinit-uni):取消接口地址最后面的 /
2023-05-14 23:21:00 +08:00

137 lines
4.4 KiB
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.

# -*- coding: utf-8 -*-
# @version : 1.0
# @Create Time : 2021/10/19 15:47
# @File : exception.py
# @IDE : PyCharm
# @desc : 全局异常处理
from fastapi.responses import JSONResponse
from starlette.exceptions import HTTPException as StarletteHTTPException
from fastapi.exceptions import RequestValidationError
from starlette import status
from fastapi import Request
from fastapi.encoders import jsonable_encoder
from fastapi import FastAPI
from core.logger import logger
class CustomException(Exception):
def __init__(
self,
msg: str,
code: int = status.HTTP_400_BAD_REQUEST,
status_code: int = status.HTTP_200_OK,
desc: str = None
):
self.msg = msg
self.code = code
self.status_code = status_code
self.desc = desc
def register_exception(app: FastAPI):
"""
异常捕捉
"""
@app.exception_handler(CustomException)
async def custom_exception_handler(request: Request, exc: CustomException):
"""
自定义异常
"""
print("请求地址", request.url.__str__())
print("捕捉到重写CustomException异常异常custom_exception_handler")
logger.error(exc.desc)
logger.error(exc.msg)
print(exc.desc)
print(exc.msg)
return JSONResponse(
status_code=exc.status_code,
content={"message": exc.msg, "code": exc.code},
)
@app.exception_handler(StarletteHTTPException)
async def unicorn_exception_handler(request: Request, exc: StarletteHTTPException):
"""
重写HTTPException异常处理器
"""
print("请求地址", request.url.__str__())
print("捕捉到重写HTTPException异常异常unicorn_exception_handler")
logger.error(exc.detail)
print(exc.detail)
return JSONResponse(
status_code=200,
content={
"code": status.HTTP_400_BAD_REQUEST,
"message": exc.detail,
}
)
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError):
"""
重写请求验证异常处理器
"""
print("请求地址", request.url.__str__())
print("捕捉到重写请求验证异常异常validation_exception_handler")
logger.error(exc.errors())
print(exc.errors())
msg = exc.errors()[0].get("msg")
if msg == "field required":
msg = "请求失败,缺少必填项!"
elif msg == "value is not a valid list":
print(exc.errors())
msg = f"类型错误,提交参数应该为列表!"
elif msg == "value is not a valid int":
msg = f"类型错误,提交参数应该为整数!"
elif msg == "value could not be parsed to a boolean":
msg = f"类型错误,提交参数应该为布尔值!"
return JSONResponse(
status_code=200,
content=jsonable_encoder(
{
"message": msg,
"body": exc.body,
"code": status.HTTP_400_BAD_REQUEST
}
),
)
@app.exception_handler(ValueError)
async def value_exception_handler(request: Request, exc: ValueError):
"""
捕获值异常
"""
print("请求地址", request.url.__str__())
print("捕捉到值异常value_exception_handler")
logger.error(exc.__str__())
print(exc.__str__())
return JSONResponse(
status_code=200,
content=jsonable_encoder(
{
"message": exc.__str__(),
"code": status.HTTP_400_BAD_REQUEST
}
),
)
@app.exception_handler(Exception)
async def all_exception_handler(request: Request, exc: Exception):
"""
捕获全部异常
"""
print("请求地址", request.url.__str__())
print("捕捉到全局异常all_exception_handler")
logger.error(exc.__str__())
return JSONResponse(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
content=jsonable_encoder(
{
"message": "接口异常!",
"code": status.HTTP_500_INTERNAL_SERVER_ERROR
}
),
)