2

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.)

vindard
  • 51
  • 4
  • `myVariable` contains an exception object that shows only the message when you try to print it. Try inspecting the value of `myVariable` in a debugger, or printing `dir(myVariable)` to see if the information you need is perhaps already present in the exception object. – user4815162342 Jul 13 '18 at 05:49
  • You can't access the other keys. In case of a failure a BlockIoAPIError raises and all you get is the error message from the response. The response itself is only passed back to you in case of a successful transaction. It's happening [here](https://github.com/BlockIo/block_io-python/blob/master/block_io/__init__.py) in the `api_call` method at the bottom of the page. – shmee Jul 13 '18 at 05:52
  • @user4815162342 nope, it's just a tuple with the error message string and a `__len__` attribute – vindard Jul 13 '18 at 06:21
  • @shmee ah yes I see it. Hmm, well that's not good for what I want. It looks like my only option now is to do the call outside of their python library and maybe pull the keys/values from there. – vindard Jul 13 '18 at 06:28
  • It sounds like a great feature request for the library. It seems quite straightforward to provide the remaining data in the exception object, as a separate attribute. – user4815162342 Jul 13 '18 at 07:09
  • @user4815162342 true, and probably more efficient than what I'm currently doing calling it again outside the blockio module using requests.get – vindard Jul 13 '18 at 07:58

0 Answers0