jessecooper / pyetrade

Python E-Trade API Wrapper
GNU General Public License v3.0
205 stars 96 forks source link

when response is empty text, simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0) #49

Closed mw66 closed 2 years ago

mw66 commented 3 years ago

when response is empty text

https://github.com/jessecooper/pyetrade/blob/master/pyetrade/order.py#L107

simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

mw66 commented 3 years ago

Also on line 204:

  req.json()

basically every time we return json, we need to check if the req.text is empty first; otherwise, we have this error.

jessecooper commented 3 years ago

Something like this and if req.text is "" it would just return that?

if resp_format == "json" and req.text != ""
...

I can see how a order list could come back blank but not sure we would expect similar responses elsewhere and have that be valid?

mw66 commented 3 years ago

if it's empty, just return valid empty json object {}.

otherwise, return regular json.

jessecooper commented 3 years ago

at that its probably best to handle the exception and on exception return {}

try:
   req.json()
except simplejson.errors.JSONDecodeError:
   return {}

What do you think?

mw66 commented 3 years ago

Catching exception may hide other (unknown) errors, which should be avoided in general.

Right now etrade's response can only be either valid json object or empty (BTW, in this case they should return {} from their server, but we cannot change their server behavior), so checking empty is good enough for this client library.

1rocketdude commented 2 years ago

Shouldn't we have a final exception catch that does a raise so we can discover new etrade "features"?