Skip to content

Commit a770618

Browse files
committed
Include the decode response content in HTTPError exceptions
1 parent 70cb4f7 commit a770618

3 files changed

Lines changed: 36 additions & 6 deletions

File tree

HISTORY.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ History
1010
minFraud Insights and Factors models. This indicates whether the IP
1111
address is on a suspected anonymizing network and belongs to a
1212
residential ISP.
13+
* ``HTTPError`` now provides the decoded response content in the
14+
``decoded_content`` attribute.
1315

1416
2.0.3 (2020-07-28)
1517
++++++++++++++++++

minfraud/errors.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,29 @@ class HTTPError(MinFraudError):
3939
4040
:type: str
4141
42+
.. attribute:: decoded_content:
43+
44+
The decoded response content
45+
46+
:type: str
47+
4248
"""
4349

50+
http_status: Optional[int]
51+
uri: Optional[str]
52+
decoded_content: Optional[str]
53+
4454
def __init__(
45-
self, message: str, http_status: Optional[int] = None, uri: Optional[str] = None
55+
self,
56+
message: str,
57+
http_status: Optional[int] = None,
58+
uri: Optional[str] = None,
59+
decoded_content: Optional[str] = None,
4660
) -> None:
4761
super().__init__(message)
4862
self.http_status = http_status
4963
self.uri = uri
64+
self.decoded_content = decoded_content
5065

5166

5267
class InvalidRequestError(MinFraudError):

minfraud/webservice.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ def _exception_for_error(
136136
if 400 <= status < 500:
137137
return self._exception_for_4xx_status(status, content_type, body, uri)
138138
if 500 <= status < 600:
139-
return self._exception_for_5xx_status(status, uri)
140-
return self._exception_for_unexpected_status(status, uri)
139+
return self._exception_for_5xx_status(status, body, uri)
140+
return self._exception_for_unexpected_status(status, body, uri)
141141

142142
def _exception_for_4xx_status(
143143
self, status: int, content_type: str, body: str, uri: str
@@ -151,13 +151,14 @@ def _exception_for_4xx_status(
151151
"""Returns exception for error responses with 4xx status codes."""
152152
if not body:
153153
return HTTPError(
154-
"Received a {0} error with no body".format(status), status, uri
154+
"Received a {0} error with no body".format(status), status, uri, body
155155
)
156156
if content_type.find("json") == -1:
157157
return HTTPError(
158158
"Received a {0} with the following " "body: {1}".format(status, body),
159159
status,
160160
uri,
161+
body,
161162
)
162163
try:
163164
decoded_body = json.loads(body)
@@ -169,6 +170,7 @@ def _exception_for_4xx_status(
169170
),
170171
status,
171172
uri,
173+
body,
172174
)
173175
else:
174176
if "code" in decoded_body and "error" in decoded_body:
@@ -180,6 +182,7 @@ def _exception_for_4xx_status(
180182
" or error keys: {0}".format(body),
181183
status,
182184
uri,
185+
body,
183186
)
184187

185188
@staticmethod
@@ -207,21 +210,31 @@ def _exception_for_web_service_error(
207210
return InvalidRequestError(message, code, status, uri)
208211

209212
@staticmethod
210-
def _exception_for_5xx_status(status: int, uri: str) -> HTTPError:
213+
def _exception_for_5xx_status(
214+
status: int,
215+
body: Optional[str],
216+
uri: str,
217+
) -> HTTPError:
211218
"""Returns exception for error response with 5xx status codes."""
212219
return HTTPError(
213220
u"Received a server error ({0}) for " u"{1}".format(status, uri),
214221
status,
215222
uri,
223+
body,
216224
)
217225

218226
@staticmethod
219-
def _exception_for_unexpected_status(status: int, uri: str) -> HTTPError:
227+
def _exception_for_unexpected_status(
228+
status: int,
229+
body: Optional[str],
230+
uri: str,
231+
) -> HTTPError:
220232
"""Returns exception for responses with unexpected status codes."""
221233
return HTTPError(
222234
u"Received an unexpected HTTP status " u"({0}) for {1}".format(status, uri),
223235
status,
224236
uri,
237+
body,
225238
)
226239

227240

0 commit comments

Comments
 (0)