定义
from enum import Enum
class StatusCode(Enum):
"""业务状态码定义"""
@property
def code(self):
"""获取业务状态码"""
return self.value[0]
@property
def msg(self):
"""获取状态码信息"""
return self.value[1]
@property
def status_code(self):
"""获取http状态码"""
return self.value[2] if len(self.value) > 2 else 200
OK = (0, 'Success')
# 1 参数验证类
PARAM_ERROR = (10001, 'Parameter Error')
NECESSARY_PARAM_ERR = (10002, 'Missing necessary parameter')
PARAM_NOT_EMPTY = (10002, 'Parameter not Empty')
# 2 项目业务类
NO_DATA = (20000, 'No data found')
# 3 数据库类
DB_ERROR = (30000, 'Database operation failed')
DB_INSERT_ERROR = (30001, 'DB create failed')
DB_UPDATE_ERROR = (30002, 'DB update failed')
DB_DEL_ERROR = (30003, 'DB delete failed')
DB_SELECT_ERROR = (30004, 'DB select failed')
KFK_CONNECT_ERROR = (31000, 'Kafka connect failed')
KFK_PRODUCER_ERROR = (31000, 'Kafka product failed')
KFK_CONSUMER_ERROR = (31000, 'Kafka consumer failed')
# 4 会话类
LOGIN_EXPIRED = (40000, 'Login expired', 401)
INVALID_TOKEN = (40001, 'Invalid token', 401)
# 5 权限类
NO_ACCESS = (50000, 'No access to interface')
# 6 网络请求类
HTTP_ERROR = (60000, 'Http request error', 400)
HEADER_ERROR = (60001, 'Request header error')
# 7 文件类
FILE_NOT_EXIT = (70000, 'File not exit')
FILE_OPEN_FAIL = (70001, 'Open file fail')
FILE_WRITE_FILE = (70002, 'Write file fail')
FILE_READ_FAIL = (70003, 'Read file fail')
FILE_TYPE_ERROR = (70004, 'File type error')
# 9 服务器类
SEVER_ERROR = (90000, 'Server error', 500)
# json 统一返回格式
def json_response(status: StatusCode, data={}, err=''):
return {'code': status.code, 'msg': f'{status.msg}.{err}', 'data': data}, status.status_code
使用
return json_response(StatusCode.SEVER_ERROR)