-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.py
53 lines (42 loc) · 1.45 KB
/
errors.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
from logger import general_log, reqs_log
class ExpiredToken(Exception):
pass
class FrequencyLimit(Exception):
pass
class InternalError(Exception):
pass
class Errors:
def __init__(self, response: dict):
self.response = response
self.is_error = False
self.code = -1
self.check()
def _get_vk_id_from_error_response(self):
params = self.response["error"]["request_params"]
for param_dict in params:
if param_dict["key"] == "user_id":
return param_dict["value"]
return None
def _is_error_in_response(self):
if 'error' in self.response:
self.is_error = True
self.code = self.response["error"]["error_code"]
return self.is_error
def check(self):
if self._is_error_in_response():
self.match_codes()
def match_codes(self):
match self.code:
case 5:
raise ExpiredToken("Update access token")
case 6:
general_log.debug(f"Too many requests per second.")
reqs_log.debug(self.response)
raise FrequencyLimit("Too many requests per second.")
case 10:
raise InternalError()
case 30:
_id = self._get_vk_id_from_error_response()
general_log.debug(f"profile {_id} is private")
case _:
general_log.debug(f"Code: {self.code}")