How can I get the entire object returned instead of just the "error_message" value when an error is thrown? (details below)
I am currently working with the block.io API and am trying to extract data from the object returned when an error is thrown for a certain API call. I am working in Python 3.6.
In the case where the call is successful, I am able to simply assign the call directly to a variable and get back the object returned.
For example:
try:
data = block_io.withdraw_from_labels(amounts='AMOUNT1,AMOUNT2,...', from_labels='LABEL1,LABEL2,...', to_addresses='ADDRESS1,ADDRESS2,...')
print(data)
if successful would return this:
{
"status" : "success",
"data" : {
"reference_id" : "<data>",
"more_signatures_needed" : true,
"inputs" : [
{
"input_no" : 0,
"signatures_needed" : 1,
"data_to_sign" : "<data>",
"signers" : [
{
"signer_address" : "<data>",
"signer_public_key" : "<data>",
"signed_data" : null
}
]
}
],
"encrypted_passphrase" : {
"signer_address" : "<data>",
"signer_public_key" : "<data>",
"passphrase" : "<data>"
}
}
}
but if not successful would move on the except case
except Exception as myVariable:
print(myVariable)
and return this:
'Cannot withdraw funds without Network Fee of 0.00000000 DOGE. Maximum withdrawable balance is 0.00000000 DOGE.'
The Challenge
That last message was taken directly from "status": "fail"
object that looks like the following. I can get this full object by doing the API call as a cURL instead of using their Python API docs. The object looks like:
{
"status" : "fail",
"data" : {
"error_message" : "Cannot withdraw funds without Network Fee of 0.00000000 DOGE. Maximum withdrawable balance is 0.00000000 DOGE.",
"available_balance" : "0.00000000",
"max_withdrawal_available" : "0.00000000",
"minimum_balance_needed" : "10.00000000",
"estimated_network_fee" : "0.00000000"
}
}
The problem is that:
except Exception as myVariable:
only assigns the value from the "error_message"
key to myVariable
.
My question is, how can I access the other keys within that object given that I can't seem to assign the results of that call within python in the case of a failure (i.e. data = <pythonic api call>
won't work), and within except
I can only seem to access the value from the "error_message"
key?
Ideally, I would like to access the entire object under the "status: "fail"
scenario somehow.
(p.s. entire project on Github here.)