Skip to content

Commit 9011489

Browse files
Try getting the error message in response_handler
1 parent 512c401 commit 9011489

File tree

2 files changed

+89
-19
lines changed

2 files changed

+89
-19
lines changed

plotly/plotly/plotly.py

+15-19
Original file line numberDiff line numberDiff line change
@@ -1308,7 +1308,19 @@ def response_handler(cls, response):
13081308
.format(url=get_config()['plotly_api_domain'])
13091309
)
13101310
else:
1311-
raise requests_exception
1311+
try:
1312+
parsed_response = response.json()
1313+
except:
1314+
parsed_response = response.content
1315+
1316+
try:
1317+
# try get a friendly error message
1318+
errors = parsed_response.get('errors')
1319+
message = errors[-1]['message']
1320+
except:
1321+
raise requests_exception
1322+
1323+
raise exceptions.PlotlyError(message)
13121324

13131325
if ('content-type' in response.headers and
13141326
'json' in response.headers['content-type'] and
@@ -1701,23 +1713,7 @@ def create_animations(figure, filename=None, sharing='public', auto_open=True):
17011713
api_url = _api_v2.api_url('plots')
17021714
r = requests.post(api_url, auth=auth, headers=headers, json=json)
17031715

1704-
try:
1705-
parsed_response = r.json()
1706-
except:
1707-
parsed_response = r.content
1708-
1709-
# raise error message
1710-
if not r.ok:
1711-
message = ''
1712-
if isinstance(parsed_response, dict):
1713-
errors = parsed_response.get('errors')
1714-
if errors and errors[-1].get('message'):
1715-
message = errors[-1]['message']
1716-
if message:
1717-
raise exceptions.PlotlyError(message)
1718-
else:
1719-
# shucks, we're stuck with a generic error...
1720-
r.raise_for_status()
1716+
parsed_response = _api_v2.response_handler(r)
17211717

17221718
if sharing == 'secret':
17231719
web_url = (parsed_response['file']['web_url'][:-1] +
@@ -1738,7 +1734,7 @@ def icreate_animations(figure, filename=None, sharing='public', auto_open=False)
17381734
This function is based off `plotly.plotly.iplot`. See `plotly.plotly.
17391735
create_animations` Doc String for param descriptions.
17401736
"""
1741-
# Still needs doing: create a wrapper for iplot and icreate_animations
1737+
# TODO: create a wrapper for iplot and icreate_animations
17421738
url = create_animations(figure, filename, sharing, auto_open)
17431739

17441740
if isinstance(figure, dict):

test.py

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import plotly.plotly as py
2+
3+
4+
from plotly.grid_objs import Grid, Column
5+
6+
column_1 = Column([1, 2, 3], 'x')
7+
column_2 = Column([1, 3, 6], 'y')
8+
column_3 = Column([2, 4, 6], 'new x')
9+
column_4 = Column([1, 1, 5], 'new y')
10+
grid = Grid([column_1, column_2, column_3, column_4])
11+
py.grid_ops.upload(grid, 'animations_grid', auto_open=False)
12+
13+
# create figure
14+
figure = {
15+
'data': [
16+
{
17+
'xsrc': grid.get_column_reference('x'),
18+
'ysrc': grid.get_column_reference('y')
19+
}
20+
],
21+
'layout': {'title': 'First Title'},
22+
'frames': [
23+
{
24+
'data': [
25+
{
26+
'xsrc': grid.get_column_reference('new x'),
27+
'ysrc': grid.get_column_reference('new y')
28+
}
29+
],
30+
'layout': {'title': 'Second Title'}
31+
}
32+
]
33+
}
34+
35+
py.icreate_animations(figure, 'new_plot_with_animations')
36+
37+
print('done 1')
38+
39+
40+
from plotly.grid_objs import Grid, Column
41+
42+
column_1 = Column([1, 2, 3], 'x')
43+
column_2 = Column([1, 3, 6], 'y')
44+
column_3 = Column([2, 4, 6], 'new x')
45+
column_4 = Column([1, 1, 5], 'new y')
46+
grid = Grid([column_1, column_2, column_3, column_4])
47+
py.grid_ops.upload(grid, 'animations_grid', auto_open=False)
48+
49+
# create figure
50+
figure = {
51+
'data': [
52+
{
53+
'xsrc': grid.get_column_reference('x'),
54+
'ysrc': grid.get_column_reference('y')
55+
}
56+
],
57+
'layout': {'title': 'First Title'},
58+
'frames': [
59+
{
60+
'data': [
61+
{
62+
'xsrc': grid.get_column_reference('new x'),
63+
'ysrc': grid.get_column_reference('new y')
64+
}
65+
],
66+
'layout': {'title': 'Second Title'}
67+
}
68+
]
69+
}
70+
71+
py.icreate_animations(figure, 'new_plot_with_animations')
72+
73+
74+
print('done 2')

0 commit comments

Comments
 (0)