Skip to content

Commit 477296f

Browse files
authored
Merge pull request #98 from maxmind/greg/v4.1.0
Prepare for 4.1.0
2 parents 2305048 + dcee069 commit 477296f

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

HISTORY.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
History
44
-------
55

6-
4.1.0
6+
4.1.0 (2020-09-25)
77
++++++++++++++++++
88

99
* Added the ``is_residential_proxy`` attribute to ``geoip2.model.AnonymousIP``
1010
and ``geoip2.record.Traits``.
11+
* ``HTTPError`` now provides the decoded response content in the
12+
``decoded_content`` attribute. Requested by Oleg Serbokryl. GitHub #95.
1113

1214
4.0.2 (2020-07-28)
1315
++++++++++++++++++

geoip2/errors.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,21 @@ class HTTPError(GeoIP2Error):
3232
3333
:ivar http_status: The HTTP status code returned
3434
:ivar uri: The URI queried
35+
:ivar decoded_content: The decoded response content
3536
3637
"""
3738

3839
def __init__(
39-
self, message: str, http_status: Optional[int] = None, uri: Optional[str] = None
40+
self,
41+
message: str,
42+
http_status: Optional[int] = None,
43+
uri: Optional[str] = None,
44+
decoded_content: Optional[str] = None,
4045
) -> None:
4146
super().__init__(message)
4247
self.http_status = http_status
4348
self.uri = uri
49+
self.decoded_content = decoded_content
4450

4551

4652
class InvalidRequestError(GeoIP2Error):

geoip2/webservice.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ def _exception_for_error(
112112
if 400 <= status < 500:
113113
return self._exception_for_4xx_status(status, content_type, body, uri)
114114
if 500 <= status < 600:
115-
return self._exception_for_5xx_status(status, uri)
116-
return self._exception_for_non_200_status(status, uri)
115+
return self._exception_for_5xx_status(status, uri, body)
116+
return self._exception_for_non_200_status(status, uri, body)
117117

118118
def _exception_for_4xx_status(
119119
self, status: int, content_type: str, body: str, uri: str
@@ -123,13 +123,15 @@ def _exception_for_4xx_status(
123123
"Received a %(status)i error for %(uri)s " "with no body." % locals(),
124124
status,
125125
uri,
126+
body,
126127
)
127128
if content_type.find("json") == -1:
128129
return HTTPError(
129130
"Received a %i for %s with the following "
130131
"body: %s" % (status, uri, str(content_type)),
131132
status,
132133
uri,
134+
body,
133135
)
134136
try:
135137
decoded_body = json.loads(body)
@@ -139,16 +141,18 @@ def _exception_for_4xx_status(
139141
" not include the expected JSON body: " % locals() + ", ".join(ex.args),
140142
status,
141143
uri,
144+
body,
142145
)
143146
else:
144-
if "code" in body and "error" in body:
147+
if "code" in decoded_body and "error" in decoded_body:
145148
return self._exception_for_web_service_error(
146149
decoded_body.get("error"), decoded_body.get("code"), status, uri
147150
)
148151
return HTTPError(
149152
"Response contains JSON but it does not specify " "code or error keys",
150153
status,
151154
uri,
155+
body,
152156
)
153157

154158
@staticmethod
@@ -180,20 +184,26 @@ def _exception_for_web_service_error(
180184
return InvalidRequestError(message, code, status, uri)
181185

182186
@staticmethod
183-
def _exception_for_5xx_status(status: int, uri: str) -> HTTPError:
187+
def _exception_for_5xx_status(
188+
status: int, uri: str, body: Optional[str]
189+
) -> HTTPError:
184190
return HTTPError(
185191
"Received a server error (%(status)i) for " "%(uri)s" % locals(),
186192
status,
187193
uri,
194+
body,
188195
)
189196

190197
@staticmethod
191-
def _exception_for_non_200_status(status: int, uri: str) -> HTTPError:
198+
def _exception_for_non_200_status(
199+
status: int, uri: str, body: Optional[str]
200+
) -> HTTPError:
192201
return HTTPError(
193202
"Received a very surprising HTTP status "
194203
"(%(status)i) for %(uri)s" % locals(),
195204
status,
196205
uri,
206+
body,
197207
)
198208

199209

0 commit comments

Comments
 (0)