Skip to content

Instantly share code, notes, and snippets.

@songtianlun
Last active January 18, 2022 09:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save songtianlun/dea18aeb803da77a1fee22f890efdfef to your computer and use it in GitHub Desktop.
Save songtianlun/dea18aeb803da77a1fee22f890efdfef to your computer and use it in GitHub Desktop.
一个 HTTP 服务错误码码枚举类,用于规范服务返回的错误信息和错误码。
#!/usr/bin/python
# coding=utf8
"""
# Author: songtianlun
# mail: songtianlun@frytea.com
# Created Time : 2022-01-18 16:49:58
# License: GPL-2.0-only
# File Name: errno.py
# Description:
HTTP 服务返回码枚举类
使用时导入 ErrCodeEnum
使用 .code 属性方法获取错误码
使用 .msg 属性方法获取错误信息
使用举例:
from mimic_daemon_server.common.errno import ErrCodeEnum
...
return jsonify({
"ret_code": ErrCodeEnum.DESTROY_VM_FAILED.code,
"error": ErrCodeEnum.DESTROY_VM_FAILED.msg,
"info": {...}
}), 200
"""
from enum import Enum
class ErrCodeEnum(Enum):
# 通用错误
OK = (0, '成功')
ERROR = (20000, '错误')
# 虚拟机错误
CREATE_VM_FAILED = (20101, '创建虚拟机失败')
DESTROY_VM_FAILED = (20102, '销毁虚拟机失败')
@property
def code(self):
return self.value[0]
@property
def msg(self):
return self.value[1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment