1
1
#!/usr/bin/env python3
2
2
# -*- coding: utf-8 -*-
3
- """
4
- 全局业务异常类
5
-
6
- 业务代码执行异常时,可以使用 raise xxxError 触发内部错误,它尽可能实现带有后台任务的异常,但它不适用于**自定义响应状态码**
7
- 如果要求使用**自定义响应状态码**,则可以通过 return response_base.fail(res=CustomResponseCode.xxx) 直接返回
8
- """ # noqa: E501
9
-
10
3
from typing import Any
11
4
12
5
from fastapi import HTTPException
16
9
17
10
18
11
class BaseExceptionMixin (Exception ):
12
+ """基础异常混入类"""
13
+
19
14
code : int
20
15
21
16
def __init__ (self , * , msg : str = None , data : Any = None , background : BackgroundTask | None = None ):
@@ -26,38 +21,50 @@ def __init__(self, *, msg: str = None, data: Any = None, background: BackgroundT
26
21
27
22
28
23
class HTTPError (HTTPException ):
24
+ """HTTP 异常"""
25
+
29
26
def __init__ (self , * , code : int , msg : Any = None , headers : dict [str , Any ] | None = None ):
30
27
super ().__init__ (status_code = code , detail = msg , headers = headers )
31
28
32
29
33
30
class CustomError (BaseExceptionMixin ):
31
+ """自定义异常"""
32
+
34
33
def __init__ (self , * , error : CustomErrorCode , data : Any = None , background : BackgroundTask | None = None ):
35
34
self .code = error .code
36
35
super ().__init__ (msg = error .msg , data = data , background = background )
37
36
38
37
39
38
class RequestError (BaseExceptionMixin ):
39
+ """请求异常"""
40
+
40
41
code = StandardResponseCode .HTTP_400
41
42
42
43
def __init__ (self , * , msg : str = 'Bad Request' , data : Any = None , background : BackgroundTask | None = None ):
43
44
super ().__init__ (msg = msg , data = data , background = background )
44
45
45
46
46
47
class ForbiddenError (BaseExceptionMixin ):
48
+ """禁止访问异常"""
49
+
47
50
code = StandardResponseCode .HTTP_403
48
51
49
52
def __init__ (self , * , msg : str = 'Forbidden' , data : Any = None , background : BackgroundTask | None = None ):
50
53
super ().__init__ (msg = msg , data = data , background = background )
51
54
52
55
53
56
class NotFoundError (BaseExceptionMixin ):
57
+ """资源不存在异常"""
58
+
54
59
code = StandardResponseCode .HTTP_404
55
60
56
61
def __init__ (self , * , msg : str = 'Not Found' , data : Any = None , background : BackgroundTask | None = None ):
57
62
super ().__init__ (msg = msg , data = data , background = background )
58
63
59
64
60
65
class ServerError (BaseExceptionMixin ):
66
+ """服务器异常"""
67
+
61
68
code = StandardResponseCode .HTTP_500
62
69
63
70
def __init__ (
@@ -67,20 +74,26 @@ def __init__(
67
74
68
75
69
76
class GatewayError (BaseExceptionMixin ):
77
+ """网关异常"""
78
+
70
79
code = StandardResponseCode .HTTP_502
71
80
72
81
def __init__ (self , * , msg : str = 'Bad Gateway' , data : Any = None , background : BackgroundTask | None = None ):
73
82
super ().__init__ (msg = msg , data = data , background = background )
74
83
75
84
76
85
class AuthorizationError (BaseExceptionMixin ):
86
+ """授权异常"""
87
+
77
88
code = StandardResponseCode .HTTP_401
78
89
79
90
def __init__ (self , * , msg : str = 'Permission Denied' , data : Any = None , background : BackgroundTask | None = None ):
80
91
super ().__init__ (msg = msg , data = data , background = background )
81
92
82
93
83
94
class TokenError (HTTPError ):
95
+ """Token 异常"""
96
+
84
97
code = StandardResponseCode .HTTP_401
85
98
86
99
def __init__ (self , * , msg : str = 'Not Authenticated' , headers : dict [str , Any ] | None = None ):
0 commit comments