-
Notifications
You must be signed in to change notification settings - Fork 0
/
exceptions.py
51 lines (40 loc) · 1.39 KB
/
exceptions.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
"""
@Author: WangYuXiang
@E-mile: [email protected]
@CreateTime: 2021/1/20 20:03
@DependencyLibrary: 无
@MainFunction:无
@FileDoc:
exceptions.py
序列化器文件
"""
from sanic_rest_framework.status import HttpStatus, RuleStatus
class ValidatorAssertError(Exception):
pass
class APIException(Exception):
def __init__(self, message, status=RuleStatus.STATUS_0_FAIL, http_status=HttpStatus.HTTP_500_INTERNAL_SERVER_ERROR,
*args, **kwargs):
self.message = message
self.status = status
self.http_status = http_status
def response_data(self):
return {
'msg': self.message,
'status': self.status,
'http_status': self.http_status
}
class ValidationException(Exception):
"""验证错误类"""
default_detail = '无效的输入'
default_code = 'invalid'
def __init__(self, error_detail=None, code=None):
if error_detail is None:
error_detail = self.default_detail
if code is None:
code = self.default_code
# For validation failures, we may collect many errors together,
# so the details should always be coerced to a list if not already.
if not isinstance(error_detail, dict) and not isinstance(error_detail, list):
error_detail = [error_detail]
self.code = code
self.error_detail = error_detail