lsymds / hetznercloud-py

Python SDK for the new Hetzner cloud
MIT License
30 stars 12 forks source link

Ensure checks for 200 status code are completed (Original title: Crash on bad status code) #9

Closed nh2 closed 6 years ago

nh2 commented 6 years ago

The code at https://github.com/elsyms/hetznercloud-py/blob/182caf09f7ed397adeb284662e6e0edcfe6b9dfa/hetznercloud/shared.py only catches some specific HTTP error status codes, continuing otherwise.

Calling code like https://github.com/elsyms/hetznercloud-py/blob/182caf09f7ed397adeb284662e6e0edcfe6b9dfa/hetznercloud/servers.py#L11-L16 also doesn't check if the response is actually a 200 status code, so as a result result["server"] crashes as error responses don't have that key.

One example of that is when you get rate limited (https://docs.hetzner.cloud/#header-rate-limiting-1), in which case the status code is 429 Too Many Requests with response body:

{u'error': {u'message': u'limit of 3600 requests per hour reached', u'code': u'rate_limit_exceeded', u'details': None}}

hetznercloud-py should not crash in such cases.

Best would be to raise a generic exception containing the HTTP status code and response body, so that this is shown to the user when the Exception bubbles up.

nh2 commented 6 years ago

(Also there should be an exception for rate limiting, as that is rather important to handle in programs using this API.)

nh2 commented 6 years ago

(Also there should be an exception for rate limiting, as that is rather important to handle in programs using this API.)

I added that in my branch, will send a PR.

nh2 commented 6 years ago

I added that in my branch, will send a PR.

11