From 0a7222e2ef59b982a846c3e5c8a7802b44396bb5 Mon Sep 17 00:00:00 2001 From: Priscila Oliveira Date: Wed, 17 Jul 2024 08:32:59 +0200 Subject: [PATCH] fix getDetails JSON error on 5xx status code --- ipinfo/handler.py | 6 +++++- ipinfo/handler_async.py | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/ipinfo/handler.py b/ipinfo/handler.py index a4afd12..507c810 100644 --- a/ipinfo/handler.py +++ b/ipinfo/handler.py @@ -123,8 +123,12 @@ def getDetails(self, ip_address=None, timeout=None): if response.status_code == 429: raise RequestQuotaExceededError() if response.status_code >= 400: - error_response = response.json() 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, error_response) details = response.json() diff --git a/ipinfo/handler_async.py b/ipinfo/handler_async.py index 242728f..c71357a 100644 --- a/ipinfo/handler_async.py +++ b/ipinfo/handler_async.py @@ -145,8 +145,12 @@ async def getDetails(self, ip_address=None, timeout=None): if resp.status == 429: raise RequestQuotaExceededError() if resp.status >= 400: - error_response = await resp.json() error_code = resp.status + content_type = resp.headers.get('Content-Type') + if content_type == 'application/json': + error_response = await resp.json() + else: + error_response = {'error': resp.text()} raise APIError(error_code, error_response) details = await resp.json()