Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions fireblocks_sdk/api_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redundant

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
Copy link
Collaborator

@asafs932 asafs932 May 22, 2024

Choose a reason for hiding this comment

The 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
check before what was the string if "response.text" was None


# Let's maintain the .message attribute for backwards compatibility purposes
self.message = display_error


class PagedVaultAccountsRequestFilters:
Expand Down
19 changes: 10 additions & 9 deletions fireblocks_sdk/sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about other types of exceptions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 {
Expand Down
Loading