This repository has been archived by the owner on Dec 19, 2024. It is now read-only.
forked from GraiaProject/Avilla
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexception.py
80 lines (58 loc) · 2.28 KB
/
exception.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import json
from typing import Mapping, Optional
from avilla.core.exceptions import ActionFailed as BaseActionFailed
from avilla.core.exceptions import HttpRequestError, InvalidAuthentication
from avilla.core.exceptions import NetworkError as BaseNetworkError
from avilla.qqapi.audit import audit_result
class ActionFailed(HttpRequestError, BaseActionFailed):
def __init__(self, status: int, headers: Mapping, response: Optional[str] = None):
self.body = {}
self.headers = headers
if response:
try:
self.body = json.loads(response)
except json.JSONDecodeError:
pass
super().__init__(status, self.body.get("message", "Unknown Error"))
@property
def code(self) -> Optional[int]:
return None if self.body is None else self.body.get("code", None)
@property
def message(self) -> Optional[str]:
return None if self.body is None else self.body.get("message", None)
@property
def data(self) -> Optional[dict]:
return None if self.body is None else self.body.get("data", None)
@property
def trace_id(self) -> Optional[str]:
return self.headers.get("X-Tps-trace-ID", None)
def __repr__(self) -> str:
args = ("code", "message", "data", "trace_id")
return (
f"<{self.__class__.__name__}: {self.status}, "
+ ", ".join(f"{k}={v}" for k in args if (v := getattr(self, k)) is not None)
+ ">"
)
class UnauthorizedException(ActionFailed, InvalidAuthentication):
pass
class RateLimitException(ActionFailed):
pass
class ApiNotAvailable(ActionFailed):
pass
class NetworkError(BaseNetworkError):
def __init__(self, msg: Optional[str] = None):
super().__init__()
self.msg: Optional[str] = msg
"""错误原因"""
def __repr__(self):
return f"<NetWorkError message={self.msg}>"
def __str__(self):
return self.__repr__()
class AuditException(Exception):
"""消息审核"""
def __init__(self, audit_id: str):
super().__init__()
self.audit_id = audit_id
async def get_audit_result(self, timeout: Optional[float] = None):
"""获取审核结果"""
return await audit_result.fetch(self.audit_id, timeout or 60)