-
Notifications
You must be signed in to change notification settings - Fork 50
/
customException.py
executable file
·63 lines (47 loc) · 1.4 KB
/
customException.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# -*- coding: utf-8 -*-
# @Time : 2019/4/19 2:46 PM
# @Author : ShaHeTop-Almighty-ares
# @Email : [email protected]
# @File : customException.py
# @Software: PyCharm
from werkzeug.exceptions import HTTPException
from flask import jsonify, abort, request
import flask_restful
custom_resp_dict = {
333: '测试自定义异常',
400: '参数类型错误',
401: '未登录_认证信息失败_令牌过期',
403: '无权限',
500: '服务器异常',
666: 'Token?',
996: '没救了'
}
class CustomException(HTTPException):
code = None
msg = None
def __init__(self, code=None, msg=None):
if code:
self.code = code
if msg:
self.msg = msg
super(CustomException, self).__init__(self.code, self.msg)
def method_view_ab_code(code):
"""MethodView 自定义异常"""
msg = custom_resp_dict.get(code, 'ERROR')
raise CustomException(code=code, msg=msg)
def flask_restful_ab_code(code):
"""Flask Restful 自定义异常"""
message = custom_resp_dict.get(code)
if message:
req = request.method + ' ' + request.path
result = {
"code": code,
"message": message,
"request": req
}
result = jsonify(result)
else:
result = code
# 修改、简化 flask_restful.abort
flask_restful.abort = abort(result)
flask_restful.abort(code)