Skip to content

Commit

Permalink
Preserve original remote error attributes in API exception
Browse files Browse the repository at this point in the history
What this fixes:
 - wild "except" statement
 - non-Pythonic direct type comparison (even worse, with "is")
 - textual FB response message is parsed and preserved; this may be
handy when trying to automatically propagate a meaningful textual error
reason somewhere upstream in the code (e.g. operator dashboard panel)
 - original HTTP status code is now preserved for easier debugging under
the http_code attribute
 - original requests status exception is nicely chained (with the
extended raise from statement)
  • Loading branch information
knaperek committed Apr 24, 2024
1 parent 6d9d903 commit 69e6e65
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
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 = '',
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)

# 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
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

0 comments on commit 69e6e65

Please sign in to comment.