RODY/app/core/err_handler.py
552068321@qq.com 6f7de660aa first commit
2022-11-04 17:37:08 +08:00

69 lines
1.6 KiB
Python

import logging
from flask import jsonify, request, make_response, abort
from marshmallow import ValidationError
logger = logging.getLogger(__name__)
# 处理404错误
def page_not_found(e):
# if a request is in our blog URL space
if request.path.startswith('/blog/'):
# we return a custom blog 404 page
return jsonify({
"code": e.code,
"message": e.name,
"description": e.description,
"data": ""
}), 404
else:
# otherwise we return our generic site-wide 404 page
return jsonify({
"code": e.code,
"message": e.name,
"description": e.description,
"data": ""
}), 404
# 处理405错误
def method_not_allowed(e):
# otherwise we return a generic site-wide 405 page
return jsonify({
"code": e.code,
"message": e.name,
"description": e.description,
"data": ""
}), 405
# 处理其他400错误
def exception_400(e):
# otherwise we return a generic site-wide 405 page
return jsonify({
"code": e.code,
"message": e.name,
"description": e.description,
"data": ""
}), 400
# 处理其他状态码错误
def exception_500(e):
print(e)
return jsonify({
"code": e.code,
"message": e.name,
"description": e.description,
"data": ""
}), e.code
# 处理参数校验错误
def check_data(schema, data):
try:
return schema().load(data)
except ValidationError as e:
abort(make_response(jsonify(code=400, message=str(e.messages), data=None), 400))