Skip to content

Commit c017d0c

Browse files
committed
resolving conflicts
2 parents 6c04a68 + d3e7eb9 commit c017d0c

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

ipinfo/error.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import json
2+
3+
4+
class APIError(Exception):
5+
def __init__(self, error_code, error_json):
6+
self.error_code = error_code
7+
self.error_json = error_json
8+
9+
def __str__(self):
10+
return f"APIError: {self.error_code}\n{json.dumps(self.error_json, indent=2)}"

ipinfo/handler.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import requests
1212

13+
from .error import APIError
1314
from .cache.default import DefaultCache
1415
from .details import Details
1516
from .exceptions import RequestQuotaExceededError, TimeoutExceededError
@@ -140,7 +141,10 @@ def getDetails(self, ip_address=None, timeout=None):
140141
response = requests.get(url, headers=headers, **req_opts)
141142
if response.status_code == 429:
142143
raise RequestQuotaExceededError()
143-
response.raise_for_status()
144+
if response.status_code >= 400:
145+
error_response = response.json()
146+
error_code = response.status_code
147+
raise APIError(error_code, error_response)
144148
details = response.json()
145149

146150
# format & cache

ipinfo/handler_async.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import aiohttp
1313

14+
from .error import APIError
1415
from .cache.default import DefaultCache
1516
from .details import Details
1617
from .exceptions import RequestQuotaExceededError, TimeoutExceededError
@@ -163,7 +164,10 @@ async def getDetails(self, ip_address=None, timeout=None):
163164
async with self.httpsess.get(url, headers=headers, **req_opts) as resp:
164165
if resp.status == 429:
165166
raise RequestQuotaExceededError()
166-
resp.raise_for_status()
167+
if resp.status >= 400:
168+
error_response = await resp.json()
169+
error_code = resp.status
170+
raise APIError(error_code, error_response)
167171
details = await resp.json()
168172

169173
# format & cache

0 commit comments

Comments
 (0)