Skip to content

Commit 2615218

Browse files
committed
Generalize our PlotlyRequestError.
This is also some setup for a larger refact. Since v1 and v2 requests handle errors differently, it’s easier if we simplify the api into our errors so that the requests can go from `response` —> `python error` as they see fit.
1 parent c8092f8 commit 2615218

File tree

2 files changed

+16
-26
lines changed

2 files changed

+16
-26
lines changed

plotly/exceptions.py

+6-23
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,12 @@ class InputError(PlotlyError):
1818

1919

2020
class PlotlyRequestError(PlotlyError):
21-
def __init__(self, requests_exception):
22-
self.status_code = requests_exception.response.status_code
23-
self.HTTPError = requests_exception
24-
content_type = requests_exception.response.headers['content-type']
25-
if 'json' in content_type:
26-
content = requests_exception.response.content
27-
if content != '':
28-
res_payload = json.loads(
29-
requests_exception.response.content.decode('utf8')
30-
)
31-
if 'detail' in res_payload:
32-
self.message = res_payload['detail']
33-
else:
34-
self.message = ''
35-
else:
36-
self.message = ''
37-
elif content_type == 'text/plain':
38-
self.message = requests_exception.response.content
39-
else:
40-
try:
41-
self.message = requests_exception.message
42-
except AttributeError:
43-
self.message = 'unknown error'
21+
"""General API error. Raised for *all* failed requests."""
22+
23+
def __init__(self, message, status_code, content):
24+
self.message = message
25+
self.status_code = status_code
26+
self.content = content
4427

4528
def __str__(self):
4629
return self.message

plotly/plotly/plotly.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -1296,10 +1296,17 @@ def response_handler(cls, response):
12961296
message = errors[-1]['message']
12971297
except:
12981298
# otherwise raise a generic error
1299-
raise exceptions.PlotlyRequestError(requests_exception)
1299+
message = 'No content'
1300+
status_code = requests_exception.status_code
1301+
content = requests_exception.content
1302+
raise exceptions.PlotlyRequestError(message, status_code,
1303+
content)
13001304
else:
1301-
requests_exception.message = message
1302-
raise exceptions.PlotlyRequestError(requests_exception)
1305+
message = requests_exception.message
1306+
status_code = requests_exception.status_code
1307+
content = requests_exception.content
1308+
raise exceptions.PlotlyRequestError(message, status_code,
1309+
content)
13031310

13041311
if ('content-type' in response.headers and
13051312
'json' in response.headers['content-type'] and

0 commit comments

Comments
 (0)