-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix authentication error payload #360
Changes from 2 commits
312185a
e82d925
a43004e
7abec3e
77311b5
6e89fa0
c36ea86
594ee65
0ac2c98
4ee8ddf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,76 +2,41 @@ | |
|
||
import httpx | ||
|
||
|
||
# Request related exceptions | ||
class BaseRequestException(Exception): | ||
def __init__( | ||
self, | ||
response: httpx.Response, | ||
message: Optional[str] = None, | ||
error: Optional[str] = None, | ||
errors: Optional[Mapping[str, Any]] = None, | ||
error_description: Optional[str] = None, | ||
code: Optional[str] = None, | ||
pending_authentication_token: Optional[str] = None, | ||
response_json: Mapping[str, Any], | ||
) -> None: | ||
super(BaseRequestException, self).__init__(message) | ||
|
||
self.message = message | ||
self.error = error | ||
self.errors = errors | ||
self.code = code | ||
self.error_description = error_description | ||
self.pending_authentication_token = pending_authentication_token | ||
self.extract_and_set_response_related_data(response) | ||
super(BaseRequestException, self).__init__(response_json) | ||
|
||
def extract_and_set_response_related_data(self, response: httpx.Response) -> None: | ||
self.response = response | ||
self.response_json = response_json | ||
|
||
try: | ||
response_json = response.json() | ||
self.message = response_json.get("message") | ||
self.error = response_json.get("error") | ||
self.errors = response_json.get("errors") | ||
self.code = response_json.get("code") | ||
self.error_description = response_json.get("error_description") | ||
self.pending_authentication_token = response_json.get( | ||
"pending_authentication_token" | ||
) | ||
except ValueError: | ||
self.message = None | ||
self.error = None | ||
self.errors = None | ||
self.code = None | ||
self.error_description = None | ||
self.pending_authentication_token = None | ||
|
||
headers = response.headers | ||
self.request_id = headers.get("X-Request-ID") | ||
self.message = self.extractFromJson("message", "No message") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is 100x nicer than There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! Also lol snake case |
||
self.error = self.extractFromJson("error", None) | ||
self.errors = self.extractFromJson("errors", None) | ||
self.code = self.extractFromJson("code", None ) | ||
self.error_description = self.extractFromJson("error_description", None) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh good catch! |
||
|
||
def __str__(self) -> str: | ||
message = self.message or "No message" | ||
exception = "(message=%s" % message | ||
self.request_id = response.headers.get("X-Request-ID") | ||
|
||
if self.request_id is not None: | ||
exception += ", request_id=%s" % self.request_id | ||
def extractFromJson(self, key: str, alt: Optional[str] = None) -> Optional[str]: | ||
if self.response_json is None: | ||
return alt | ||
|
||
if self.code is not None: | ||
exception += ", code=%s" % self.code | ||
return self.response_json.get(key, alt) | ||
|
||
if self.error is not None: | ||
exception += ", error=%s" % self.error | ||
|
||
if self.errors is not None: | ||
exception += ", errors=%s" % self.errors | ||
def __str__(self) -> str: | ||
exception = "(message=%s" % self.message | ||
|
||
if self.error_description is not None: | ||
exception += ", error_description=%s" % self.error_description | ||
if self.response_json is not None: | ||
for key, value in self.response_json.items(): | ||
if key != "message": | ||
exception += ", %s=%s" % (key, value) | ||
|
||
if self.pending_authentication_token is not None: | ||
exception += ( | ||
", pending_authentication_token=%s" % self.pending_authentication_token | ||
) | ||
exception += ", request_id=%s" % self.request_id | ||
|
||
return exception + ")" | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What we want is for everything in the return value to be added to exceptions, this test does the opposite.