Throwing exceptions on 400s and 500s in _do_request #5
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is an extension to #3. Basically, the code currently doesn't throw an exception based on the response code actually given. A good example is calling get_app- if the Marathon API returns either a 400 or 500, the response won't contain the 'app' key and the actual exception will be a KeyError occurring on line 52 of client.py when it tries to retrieve this key. That's pretty bad, as this means I can't tell when I've gotten something like this in my code:
WARNING:marathon:Got HTTP 504: {"message":"Failed to wait for future within timeout"}
This gets really ugly in the code I've written to deploy new marathon apps- I use get_app to determine whether or not the current version of an app has been deployed, and if I see a KeyError, I assume it wasn't deployed and attempt to look up old versions of it and blue-green the new app with the old ones. If I got a KeyError due to a 500, then I have no idea that the app might already be there, but currently have to assume that it isn't!
So, let's use those awesome exceptions in exceptions.py! NotFoundError looks aptly named for 400s, and I added an InternalServerError exception for 500s. They're thrown when Marathon responds with a 400 or 500, meaning it's possible for callers to know what failure actually occurred.
As an aside, scale_app might not want to squelch NotFoundErrors, as the caller probably should get an exception if the app_id was invalid.