-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy patherrors.py
More file actions
138 lines (97 loc) · 4.91 KB
/
errors.py
File metadata and controls
138 lines (97 loc) · 4.91 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
from typing import TYPE_CHECKING
from typing import Any
if TYPE_CHECKING:
from scim2_models import Error
class SCIMClientError(Exception):
"""Base exception for scim2-client.
:param message: The exception reason.
:param source: The request payload or the response object that have
caused the exception.
"""
def __init__(
self, message: str, source: Any = None, *args: Any, **kwargs: Any
) -> None:
self.message = message
self.source = source
super().__init__(*args, **kwargs)
def __str__(self) -> str:
return self.message or "UNKNOWN"
class SCIMRequestError(SCIMClientError):
"""Base exception for errors happening during request payload building."""
class RequestNetworkError(SCIMRequestError):
"""Error raised when a network error happened during request.
This error is raised when a :class:`httpx.RequestError` has been caught while performing a request.
The original :class:`~httpx.RequestError` is available with :attr:`~BaseException.__cause__`.
"""
def __init__(self, *args: Any, **kwargs: Any) -> None:
message = kwargs.pop("message", "Network error happened during request")
super().__init__(message, *args, **kwargs)
class RequestPayloadValidationError(SCIMRequestError):
"""Error raised when an invalid request payload has been passed to SCIMClient.
This error is raised when a :class:`pydantic.ValidationError` has been caught
while validating the client request payload.
The original :class:`~pydantic.ValidationError` is available with :attr:`~BaseException.__cause__`.
.. code-block:: python
try:
scim.create(
{
"schemas": ["urn:ietf:params:scim:schemas:core:2.0:User"],
"active": "not-a-bool",
}
)
except RequestPayloadValidationError as exc:
print("Original validation error cause", exc.__cause__)
"""
def __init__(self, *args: Any, **kwargs: Any) -> None:
message = kwargs.pop("message", "Server request payload validation error")
super().__init__(message, *args, **kwargs)
class SCIMResponseError(SCIMClientError):
"""Base exception for errors happening during response payload validation."""
class SCIMResponseErrorObject(SCIMResponseError):
"""The server response returned a :class:`scim2_models.Error` object.
Those errors are only raised when the :code:`raise_scim_errors` parameter is :data:`True`.
:param error: The :class:`~scim2_models.Error` object returned by the server.
"""
def __init__(self, error: "Error", *args: Any, **kwargs: Any) -> None:
self._error = error
parts = []
if error.scim_type:
parts.append(error.scim_type + ":")
if error.detail:
parts.append(error.detail)
message = " ".join(parts) if parts else "SCIM Error"
super().__init__(message, *args, **kwargs)
def to_error(self) -> "Error":
"""Return the :class:`~scim2_models.Error` object returned by the server."""
return self._error
class UnexpectedStatusCode(SCIMResponseError):
"""Error raised when a server returned an unexpected status code for a given :class:`~scim2_models.Context`."""
def __init__(self, status_code: int, *args: Any, **kwargs: Any) -> None:
message = kwargs.pop(
"message", f"Unexpected response status code: {status_code}"
)
super().__init__(message, *args, **kwargs)
class UnexpectedContentType(SCIMResponseError):
"""Error raised when a server returned an unexpected `Content-Type` header in a response."""
def __init__(self, content_type: str, *args: Any, **kwargs: Any) -> None:
message = kwargs.pop("message", f"Unexpected content type: {content_type}")
super().__init__(message, *args, **kwargs)
class UnexpectedContentFormat(SCIMResponseError):
"""Error raised when a server returned a response in a non-JSON format."""
def __init__(self, *args: Any, **kwargs: Any) -> None:
message = kwargs.pop("message", "Unexpected response content format")
super().__init__(message, *args, **kwargs)
class ResponsePayloadValidationError(SCIMResponseError):
"""Error raised when the server returned a payload that cannot be validated.
This error is raised when a :class:`pydantic.ValidationError` has been caught
while validating the server response payload.
The original :class:`~pydantic.ValidationError` is available with :attr:`~BaseException.__cause__`.
.. code-block:: python
try:
scim.query(User, "foobar")
except ResponsePayloadValidationError as exc:
print("Original validation error cause", exc.__cause__)
"""
def __init__(self, *args: Any, **kwargs: Any) -> None:
message = kwargs.pop("message", "Server response payload validation error")
super().__init__(message, *args, **kwargs)