新建error.py,封装错误异常
from flask import json
from werkzeug.exceptions import HTTPException
class APIException(HTTPException):
status = 500 # 错误状态码500服务器产生一个未知的错误
msg = "操作异常"
code = -1 # 错误代码,未知错误
# 设置构造函数,改变默认值
# headers是HTTP的头信息
def __init__(self, msg=None, status=None, code=None):
# 判断传了参数,用传的参数替代默认参数
if code:
self.code = code
if status:
self.status = status
if msg:
self.msg = msg
# 使用super继承HTTPException的构造方法
super(APIException, self).__init__