forked from EbbLabs/python-tidal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexceptions.py
More file actions
86 lines (51 loc) · 1.76 KB
/
exceptions.py
File metadata and controls
86 lines (51 loc) · 1.76 KB
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
81
82
83
84
85
86
from __future__ import annotations
import json
import logging
from requests import HTTPError
log = logging.getLogger(__name__)
class TidalAPIError(Exception):
pass
class AuthenticationError(TidalAPIError):
pass
class AssetNotAvailable(TidalAPIError):
pass
class TooManyRequests(TidalAPIError):
retry_after: int
def __init__(self, message: str = "Too many requests", retry_after: int = -1):
super().__init__(message)
self.retry_after = retry_after
class URLNotAvailable(TidalAPIError):
pass
class StreamNotAvailable(TidalAPIError):
pass
class MetadataNotAvailable(TidalAPIError):
pass
class ObjectNotFound(TidalAPIError):
pass
class UnknownManifestFormat(TidalAPIError):
pass
class ManifestDecodeError(TidalAPIError):
pass
class MPDNotAvailableError(TidalAPIError):
pass
class InvalidISRC(TidalAPIError):
pass
class InvalidUPC(TidalAPIError):
pass
def http_error_to_tidal_error(http_error: HTTPError) -> TidalAPIError | None:
response = http_error.response
if response.content:
json_data = response.json()
# Make sure request response contains the detailed error message
if "errors" in json_data:
log.debug("Request response: '%s'", json_data["errors"][0]["detail"])
elif "userMessage" in json_data:
log.debug("Request response: '%s'", json_data["userMessage"])
else:
log.debug("Request response: '%s'", json.dumps(json_data))
elif response.status_code == 404:
return ObjectNotFound("Object not found")
elif response.status_code == 429:
retry_after = int(response.headers.get("Retry-After", -1))
return TooManyRequests("Too many requests", retry_after=retry_after)
return None