|
1
|
|
|
from typing import Any |
|
2
|
|
|
|
|
3
|
|
|
from requests.exceptions import ConnectionError |
|
4
|
|
|
|
|
5
|
|
|
from .ensembl_config import ensembl_http_status_codes |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
class EnsemblRestError(Exception): |
|
9
|
|
|
""" |
|
10
|
|
|
Generic error class, catch-all for most EnsemblRest issues. |
|
11
|
|
|
Special cases are handled by EnsemblRestRateLimitError and EnsemblRestServiceUnavailable. |
|
12
|
|
|
""" |
|
13
|
|
|
|
|
14
|
|
|
def __init__( |
|
15
|
|
|
self, |
|
16
|
|
|
msg: str | ConnectionError, |
|
17
|
|
|
error_code: int | None = None, |
|
18
|
|
|
rate_reset: int | None = None, |
|
19
|
|
|
rate_limit: int | None = None, |
|
20
|
|
|
rate_remaining: int | None = None, |
|
21
|
|
|
retry_after: float | None = None, |
|
22
|
|
|
) -> None: |
|
23
|
|
|
self.error_code = error_code |
|
24
|
|
|
|
|
25
|
|
|
if error_code is not None and error_code in ensembl_http_status_codes: |
|
26
|
|
|
msg = "EnsEMBL REST API returned a %s (%s): %s" % ( |
|
27
|
|
|
error_code, |
|
28
|
|
|
ensembl_http_status_codes[error_code][0], |
|
29
|
|
|
msg, |
|
30
|
|
|
) |
|
31
|
|
|
|
|
32
|
|
|
super(EnsemblRestError, self).__init__(msg) |
|
33
|
|
|
|
|
34
|
|
|
@property |
|
35
|
|
|
def msg(self) -> Any: |
|
36
|
|
|
return self.args[0] |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
class EnsemblRestRateLimitError(EnsemblRestError): |
|
40
|
|
|
""" |
|
41
|
|
|
Raised when you've hit a rate limit. |
|
42
|
|
|
The amount of seconds to retry your request in will be appended to the message. |
|
43
|
|
|
""" |
|
44
|
|
|
|
|
45
|
|
|
def __init__( |
|
46
|
|
|
self, |
|
47
|
|
|
msg: str | ConnectionError, |
|
48
|
|
|
error_code: int | None = None, |
|
49
|
|
|
rate_reset: int | None = None, |
|
50
|
|
|
rate_limit: int | None = None, |
|
51
|
|
|
rate_remaining: int | None = None, |
|
52
|
|
|
retry_after: float | None = None, |
|
53
|
|
|
) -> None: |
|
54
|
|
|
if isinstance(retry_after, float): |
|
55
|
|
|
msg = "%s (Rate limit hit: Retry after %d seconds)" % (msg, retry_after) |
|
56
|
|
|
|
|
57
|
|
|
EnsemblRestError.__init__(self, msg, error_code=error_code) |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
class EnsemblRestServiceUnavailable(EnsemblRestError): |
|
61
|
|
|
""" |
|
62
|
|
|
Raised when the service is down. |
|
63
|
|
|
""" |
|
64
|
|
|
|
|
65
|
|
|
pass |
|
66
|
|
|
|