Skip to content

Commit 0a7222e

Browse files
PriOliveiramax-ipinfo
authored andcommitted
fix getDetails JSON error on 5xx status code
1 parent e1c2d5f commit 0a7222e

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

ipinfo/handler.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,12 @@ def getDetails(self, ip_address=None, timeout=None):
123123
if response.status_code == 429:
124124
raise RequestQuotaExceededError()
125125
if response.status_code >= 400:
126-
error_response = response.json()
127126
error_code = response.status_code
127+
content_type = response.headers.get('Content-Type')
128+
if content_type == 'application/json':
129+
error_response = response.json()
130+
else:
131+
error_response = {'error': response.text}
128132
raise APIError(error_code, error_response)
129133
details = response.json()
130134

ipinfo/handler_async.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,12 @@ async def getDetails(self, ip_address=None, timeout=None):
145145
if resp.status == 429:
146146
raise RequestQuotaExceededError()
147147
if resp.status >= 400:
148-
error_response = await resp.json()
149148
error_code = resp.status
149+
content_type = resp.headers.get('Content-Type')
150+
if content_type == 'application/json':
151+
error_response = await resp.json()
152+
else:
153+
error_response = {'error': resp.text()}
150154
raise APIError(error_code, error_response)
151155
details = await resp.json()
152156

0 commit comments

Comments
 (0)