Handle getDetails JSON error on 5xx status code#108
Handle getDetails JSON error on 5xx status code#108max-ipinfo merged 2 commits intoipinfo:masterfrom
Conversation
|
Thank you very much for the PR, Priscila. We really appreciate your help! I did not see the original issue; my apologies for the late response. @max-ipinfo, can you kindly review the original issue (#107) and this PR, please? Everything looks good to me. I have documented our API error codes here, if you want to take a look: https://ipinfo.io/developers/openapi.yaml Thank you. |
max-ipinfo
left a comment
There was a problem hiding this comment.
Thank you @PriOliveira for filing #107 and for sending a PR for this change! Your solution definitely improves the robustness of our library.
I would like to generalize and improve your solution further by explicitly handling non-JSON responses. Could you verify that it works on your end?
ipinfo/handler.py
Outdated
| if response.status_code == 429: | ||
| raise RequestQuotaExceededError() | ||
| if response.status_code >= 400: | ||
| if response.status_code >= 500: |
There was a problem hiding this comment.
The only issue with this approach is that we lose the error details when response.status_code >= 500.
A better approach is to explicitly handle the response content type and extract the error message.
Could you change the lines (and the same in handler_async.py) to:
if response.status_code == 429:
raise RequestQuotaExceededError()
if response.status_code >= 400:
error_code = response.status_code
content_type = response.headers.get('Content-Type')
if content_type == 'application/json':
error_response = response.json()
else:
error_response = {'error': response.text}
raise APIError(error_code, json.dumps(error_response))
Thanks!
There was a problem hiding this comment.
Sure, good point. I'll update.
00c9612 to
499a7b2
Compare
ipinfo/handler.py
Outdated
| error_response = response.json() | ||
| error_code = response.status_code | ||
|
|
||
| content_type = response.headers.get('content-type') |
There was a problem hiding this comment.
Two final formatting/linting nitpicks:
- remove blank lines 127 and 134 (and lines 149 and 156 in
handler_async.py) - even though HTTP headers are case-insensitive, we prefer to use the capitalized form in our code:
content_type = response.headers.get('Content-Type')
Thanks!
There was a problem hiding this comment.
For the header, I was trying to follow the existing pattern on those files. I only saw examples with lower case there.
There was a problem hiding this comment.
Sorry for the confusion, @PriOliveira 🤦 ! I'll update the older lowercase convention in our code in a follow-up PR.
|
I added some tests to try to cover those cases, hope you don't mind. |
|
First off, sorry for my lack of context @PriOliveira. I am just starting with this repository and do not have a good understanding of the current conventions. Bear with me 😄
I ran Do these tests pass on your end? Could you share the module versions you are using? I will most likely migrate to Poetry and upgrade dependency versions in the future. Thanks again! |
|
I ran Sorry! So feel free to only fix the following errors: |
|
I dug further and noticed that our CI pipeline and After switching to v3.10 and running I tested your changes and all looks good now: |
max-ipinfo
left a comment
There was a problem hiding this comment.
Changes look good and tests pass with Python 3.10.
Thanks again @PriOliveira !
|
I'm glad that in the end you figured out that it needs Python 3.10. Sorry that I wasn't there to point that out earlier. Thanks a lot for the review, @max-ipinfo :) |
Should solve #107