Skip to content

Commit 71676a3

Browse files
committed
handling error body
1 parent 45b3378 commit 71676a3

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

Diff for: ipinfo/error.py

+10
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)}"

Diff for: ipinfo/handler.py

+5-1
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

0 commit comments

Comments
 (0)