Skip to content

Commit 0fc6aa2

Browse files
committed
baseapi: create a ResponseError to grant access to the failing response
1 parent 023f839 commit 0fc6aa2

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

packet/baseapi.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,21 @@ def cause(self):
1919
return self._cause
2020

2121

22+
class ResponseError(Error):
23+
def __init__(self, resp, data, exception=None):
24+
if not data:
25+
msg = "(empty response)"
26+
elif "errors" in data:
27+
msg = ", ".join(data["errors"])
28+
super().__init__("Error {0}: {1}".format(resp.status_code, msg), exception)
29+
self._response = resp
30+
31+
@property
32+
def response(self):
33+
"""The Requests response which failed"""
34+
return self._response
35+
36+
2237
class JSONReadError(Error):
2338
pass
2439

@@ -84,17 +99,12 @@ def call_api(self, method, type="GET", params=None): # noqa
8499
data = resp.content # pragma: no cover
85100

86101
if not resp.ok: # pragma: no cover
87-
msg = data
88-
if not data:
89-
msg = "(empty response)"
90-
elif "errors" in data:
91-
msg = ", ".join(data["errors"])
92-
raise Error("Error {0}: {1}".format(resp.status_code, msg))
102+
raise ResponseError(resp, data)
93103

94104
try:
95105
resp.raise_for_status()
96106
except requests.HTTPError as e: # pragma: no cover
97-
raise Error("Error {0}: {1}".format(resp.status_code, resp.reason), e)
107+
raise ResponseError(resp, data, e)
98108

99109
self.meta = None
100110
try:

0 commit comments

Comments
 (0)