这时候就无法捕获异常,直接 Internal Server Error,没有进来这个方法。
class PermissionException(Exception):
def __init__(self, body):
self.body = body
class DemoException(PermissionException):
def __init__(self):
Exception.__init__(self,self.body)
def register_exception(app: FastAPI):
@user1r(DemoException)
async def all_exception_handler(request: Request, exc: DemoException):
# #query参数:
# query_data = request.query_params
# print(query_data)
print(exc.body)
log_msg = f"捕获到系统错误:请求路径:{request.url.path}\n错误信息:{traceback.format_exc()}"
mylog.error(log_msg)
return JSONResponse(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
content=jsonable_encoder({
"responseCode": Status.FAIL.get_code(),
"responseMsg": Status.FAIL.get_msg()
},
))
用了你的方法还是不行,求解。。。