Skip to content

Commit

Permalink
Even in the face of urllib.error.HTTPError, return the json
Browse files Browse the repository at this point in the history
Previously, if we caught a urllib.error.HTTPError, we would try to
parse the json response. If we succeeded we would return _only_ the
`message` field of that response. If we failed, we're return the text
of the response.  By "return" here, I actually mean "pack up into an
Exception object and throw it".

Now, if succeed in parsing the response as json, we extend that
exception object so that it also contains the JSON that we got. That
way, client code can find extra goodies in the json["data"] field.
  • Loading branch information
jannotti committed Dec 11, 2023
1 parent ef2131e commit 33cad61
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
3 changes: 2 additions & 1 deletion algosdk/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,10 @@ def __init__(self, msg):


class AlgodHTTPError(Exception):
def __init__(self, msg, code=None):
def __init__(self, msg, code=None, j=None):
super().__init__(msg)
self.code = code
self.json = j


class AlgodResponseError(Exception):
Expand Down
7 changes: 5 additions & 2 deletions algosdk/v2client/algod.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,13 @@ def algod_request(
except urllib.error.HTTPError as e:
code = e.code
es = e.read().decode("utf-8")
m = e # If json.loads() fails, we'll return e itself
j = None
try:
e = json.loads(es)["message"]
j = json.loads(es)
m = j["message"]
finally:
raise error.AlgodHTTPError(e, code)
raise error.AlgodHTTPError(m, code, j)
if response_format == "json":
try:
return json.load(resp)
Expand Down

0 comments on commit 33cad61

Please sign in to comment.