-
Notifications
You must be signed in to change notification settings - Fork 239
/
errors.py
102 lines (70 loc) · 2.99 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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
from http import HTTPStatus
import requests
class MLRunBaseError(Exception):
"""
A base class from which all other exceptions inherit.
If you want to catch all errors that the MLRun SDK might raise,
catch this base exception.
"""
pass
class MLRunHTTPError(MLRunBaseError, requests.HTTPError):
def __init__(
self, message: str, response: requests.Response = None, status_code: int = None
):
# because response object is probably with an error, it returns False, so we
# should use 'is None' specifically
if response is None:
response = requests.Response()
if status_code:
response.status_code = status_code
requests.HTTPError.__init__(self, message, response=response)
class MLRunHTTPStatusError(MLRunHTTPError):
"""
When an error has a matching http status code it is "HTTP statusable"
HTTP Status errors should inherit from this class and set the right status code in the
error_status_code attribute
"""
error_status_code = None
def __init__(self, message: str, response: requests.Response = None):
super(MLRunHTTPStatusError, self).__init__(
message, response=response, status_code=self.error_status_code
)
def raise_for_status(response: requests.Response):
"""
Raise a specific MLRunSDK error depending on the given response status code.
If no specific error exists, raises an MLRunHTTPError
"""
try:
response.raise_for_status()
except requests.HTTPError as exc:
try:
raise STATUS_ERRORS[response.status_code](
str(exc), response=response
) from exc
except KeyError:
raise MLRunHTTPError(str(exc), response=response) from exc
# Specific Errors
class MLRunUnauthorizedError(MLRunHTTPStatusError):
error_status_code = HTTPStatus.UNAUTHORIZED.value
class MLRunAccessDeniedError(MLRunHTTPStatusError):
error_status_code = HTTPStatus.FORBIDDEN.value
class MLRunNotFoundError(MLRunHTTPStatusError):
error_status_code = HTTPStatus.NOT_FOUND.value
class MLRunBadRequestError(MLRunHTTPStatusError):
error_status_code = HTTPStatus.BAD_REQUEST.value
class MLRunInvalidArgumentError(MLRunHTTPStatusError, ValueError):
error_status_code = HTTPStatus.BAD_REQUEST.value
class MLRunConflictError(MLRunHTTPStatusError):
error_status_code = HTTPStatus.CONFLICT.value
class MLRunPreconditionFailedError(MLRunHTTPStatusError):
error_status_code = HTTPStatus.PRECONDITION_FAILED.value
class MLRunIncompatibleVersionError(MLRunHTTPStatusError):
error_status_code = HTTPStatus.BAD_REQUEST.value
STATUS_ERRORS = {
HTTPStatus.BAD_REQUEST.value: MLRunBadRequestError,
HTTPStatus.UNAUTHORIZED.value: MLRunUnauthorizedError,
HTTPStatus.FORBIDDEN.value: MLRunAccessDeniedError,
HTTPStatus.NOT_FOUND.value: MLRunNotFoundError,
HTTPStatus.CONFLICT.value: MLRunConflictError,
HTTPStatus.PRECONDITION_FAILED.value: MLRunPreconditionFailedError,
}