forked from workos/workos-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexceptions.py
More file actions
83 lines (56 loc) · 2.28 KB
/
exceptions.py
File metadata and controls
83 lines (56 loc) · 2.28 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
from typing import Any, Mapping, Optional
import httpx
# Request related exceptions
class BaseRequestException(Exception):
def __init__(
self,
response: httpx.Response,
response_json: Optional[Mapping[str, Any]],
) -> None:
super(BaseRequestException, self).__init__(response_json)
self.response = response
self.response_json = response_json
self.message = self.extract_from_json("message", "No message")
self.error = self.extract_from_json("error", "Unknown")
self.errors = self.extract_from_json("errors", None)
self.code = self.extract_from_json("code", None)
self.error_description = self.extract_from_json("error_description", "Unknown")
self.request_id = response.headers.get("X-Request-ID")
def extract_from_json(self, key: str, alt: Optional[str] = None) -> Optional[str]:
if self.response_json is None:
return alt
return self.response_json.get(key, alt)
def __str__(self) -> str:
exception = "(message=%s" % self.message
exception += ", request_id=%s" % self.request_id
if self.response_json is not None:
for key, value in self.response_json.items():
if key != "message":
exception += ", %s=%s" % (key, value)
return exception + ")"
class AuthorizationException(BaseRequestException):
pass
class EmailVerificationRequiredException(AuthorizationException):
"""Raised when email verification is required before authentication.
This exception includes an email_verification_id field that can be used
to retrieve the email verification object or resend the verification email.
"""
def __init__(
self,
response: httpx.Response,
response_json: Optional[Mapping[str, Any]],
) -> None:
super().__init__(response, response_json)
self.email_verification_id = self.extract_from_json(
"email_verification_id", None
)
class AuthenticationException(BaseRequestException):
pass
class BadRequestException(BaseRequestException):
pass
class ConflictException(BaseRequestException):
pass
class NotFoundException(BaseRequestException):
pass
class ServerException(BaseRequestException):
pass