ryanmcgrath/twython — pull request #110 by jvanasco (2012-08-01 17:40:36+00:00)
Title: better handling of API errors

I kept getting Twitter errors that said 'Response was not valid JSON, unable to decode.'

After a bit of debugging, I found out why -- my app uses an environment.ini file to change between dev and production modes -- with corresponding API keys.

The current code will immediately raise an error on a failed decode -- however Twitter provides the 'unauthorized' info in the response header ( a bunch of other codes would fall in there too ).

This patch is pretty simple:

1_ it adds a 'json_error' variable, which is set to True on a failed load -- and defers raising an error for a bit...
2_ the current response.status_code check is left in tact
3_ it adds a block 'if json_error' to raise an error after the other error processing occurs

This could probably be cleaner, but then I'd have to rewrite a lot of stuff. this just works, right, and was fast.