-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy patherrors.py
More file actions
76 lines (55 loc) · 2.38 KB
/
errors.py
File metadata and controls
76 lines (55 loc) · 2.38 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
from requests import Response
import json
from urllib.parse import urlparse
class FelderaError(Exception):
"""
Generic class for Feldera error handling
"""
def __init__(self, message: str) -> None:
self.message = message
super().__init__(self.message)
def __str__(self) -> str:
return f"FelderaError. Error message: {self.message}"
class FelderaAPIError(FelderaError):
"""Error sent by Feldera API"""
def __init__(self, error: str, request: Response) -> None:
self.status_code = request.status_code
self.error = error
self.error_code = None
self.message = None
self.details = None
err_msg = ""
if request.text:
try:
json_data = json.loads(request.text)
self.error_code = json_data.get("error_code")
if self.error_code:
err_msg += f"\nError Code: {self.error_code}"
self.message = json_data.get("message")
if self.message:
err_msg += f"\nMessage: {self.message}"
self.details = json_data.get("details")
if self.details:
err_msg += f"\nDetails: {self.details}"
except Exception:
self.message = request.text
err_msg += request.text
err_msg += f"\nResponse Status: {request.status_code}"
if int(request.status_code) == 401:
parsed = urlparse(request.request.url)
auth_err = f"\nAuthorization error: Failed to connect to '{parsed.scheme}://{parsed.hostname}': "
auth = request.request.headers.get("Authorization")
if auth is None:
err_msg += f"{auth_err} API key not set"
else:
err_msg += f"{auth_err} invalid API key"
err_msg = err_msg.strip()
super().__init__(err_msg)
class FelderaTimeoutError(FelderaError):
"""Error when Feldera operation takes longer than expected"""
def __init__(self, err: str) -> None:
super().__init__(f"Timeout connecting to Feldera: {err}")
class FelderaCommunicationError(FelderaError):
"""Error when connection to Feldera"""
def __init__(self, err: str) -> None:
super().__init__(f"Cannot connect to Feldera API: {err}")