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

algod: Even in the face of urllib.error.HTTPError, return the json #529

Merged
merged 1 commit into from
Jun 5, 2024
Merged
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
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, data=None):
super().__init__(msg)
self.code = code
self.data = data


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 = {}
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.get("data"))
if response_format == "json":
try:
return json.load(resp)
Expand Down
20 changes: 20 additions & 0 deletions examples/inspect-error.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from algosdk import error, transaction

from utils import get_algod_client, algod_env

algod = get_algod_client(*algod_env())

# This program is "#pragma version 5, +". It will fail because no arguments are on the stack.
lsig = transaction.LogicSigAccount(b"\x05\x08")
sender = lsig.address()
# Get suggested parameters
params = algod.suggested_params()

amount = 10000
txn = transaction.PaymentTxn(sender, params, sender, amount)
lstx = transaction.LogicSigTransaction(txn, lsig)
try:
txid = algod.send_transaction(lstx)
print("Impossible! Exception will have been thrown")
except error.AlgodHTTPError as e:
print(e.data)
17 changes: 17 additions & 0 deletions examples/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,23 @@ class SandboxAccount:
signer: AccountTransactionSigner


def algod_env():
algodata = os.environ.get("ALGORAND_DATA")
if not algodata:
return ()
try:
token = (
open(os.path.join(algodata, "algod.token"), "rt").read().strip()
)
net = (
"http://"
+ open(os.path.join(algodata, "algod.net"), "rt").read().strip()
)
return (net, token)
except FileNotFoundError:
return ()


def get_accounts(
kmd_address: str = KMD_URL,
kmd_token: str = KMD_TOKEN,
Expand Down
Loading