-
Notifications
You must be signed in to change notification settings - Fork 41
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
Preserve original remote error attributes in API exception #159
base: master
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
|
@@ -216,14 +216,28 @@ class FireblocksApiException(Exception): | |
"""Exception raised for Fireblocks sdk errors | ||
|
||
Attributes: | ||
message: explanation of the error | ||
reason: explanation of the error | ||
error_message: original remote error message | ||
error_code: error code of the error | ||
http_code: HTTP status code of the response | ||
""" | ||
|
||
def __init__(self, message="Fireblocks SDK error", error_code=None): | ||
self.message = message | ||
def __init__(self, | ||
reason: str = '', | ||
error_message: Optional[str] = None, | ||
error_code: Optional[int] = None, | ||
http_code: Optional[int] = None): | ||
self.error_message = error_message | ||
self.error_code = error_code | ||
super().__init__(self.message) | ||
self.http_code = http_code | ||
|
||
display_error = reason or \ | ||
(error_message and f"Got an error from fireblocks server: {error_message}") or \ | ||
"Fireblocks SDK error" | ||
|
||
super().__init__(display_error) | ||
Comment on lines
+233
to
+237
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. please fix without "reason" and make sure it is the exact same string as previous to preserve backward compatibility |
||
|
||
# Let's maintain the .message attribute for backwards compatibility purposes | ||
self.message = display_error | ||
|
||
|
||
class PagedVaultAccountsRequestFilters: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,18 +57,19 @@ | |
def handle_response(response, page_mode=False): | ||
try: | ||
response_data = response.json() | ||
except: | ||
except (AttributeError, ValueError): | ||
response_data = None | ||
Comment on lines
+60
to
61
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. what about other types of exceptions? 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. Which other types of exceptions do you have in mind that would be appropriate to silence and automatically handle this way? As it is, the wild except statement would catch all exceptions, including system-related errors that have nothing to do with the Fireblocks library itself (e.g. program could be terminated, out of memory, etc..). In such cases it is more appropriate to propagate the exception rather than silencing it this way. Wild except statements are very discouraged in general and are considered bad practice in Python. |
||
if response.status_code >= 300: | ||
if type(response_data) is dict: | ||
error_code = response_data.get("code") | ||
|
||
try: | ||
response.raise_for_status() | ||
except requests.exceptions.HTTPError as err: | ||
if isinstance(response_data, dict): | ||
error_code = response_data.get('code') | ||
message = response_data.get('message') | ||
raise FireblocksApiException( | ||
"Got an error from fireblocks server: " + response.text, error_code | ||
) | ||
error_message=message, error_code=error_code, http_code=response.status_code) from err | ||
else: | ||
raise FireblocksApiException( | ||
"Got an error from fireblocks server: " + response.text | ||
) | ||
raise FireblocksApiException(error_message=response.text, http_code=response.status_code) from err | ||
else: | ||
if page_mode: | ||
return { | ||
|
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.
redundant