Skip to content

Add a nice error message when people animate before plotting #1088

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/plot_api/plot_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2156,7 +2156,11 @@ Plotly.animate = function(gd, frameOrGroupNameOrFrameList, animationOpts) {
gd = helpers.getGraphDiv(gd);

if(!Lib.isPlotDiv(gd)) {
throw new Error('This element is not a Plotly plot: ' + gd);
throw new Error(
'This element is not a Plotly plot: ' + gd + '. It\'s likely that you\'ve failed ' +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should really start putting all these error messages in a some src/constants/ file.

But, for now, this is great !

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. I think it'd be nice in general to beef up the error messages, particularly when the input looks fishy—like passing an object where an an array is expected and instead of failing it just doesn't get any props from it.

'to create a plot before animating it. For more details, see ' +
'https://plot.ly/javascript/animations/'
);
}

var trans = gd._transitionData;
Expand Down Expand Up @@ -2469,7 +2473,11 @@ Plotly.addFrames = function(gd, frameList, indices) {
}

if(!Lib.isPlotDiv(gd)) {
throw new Error('This element is not a Plotly plot: ' + gd);
throw new Error(
'This element is not a Plotly plot: ' + gd + '. It\'s likely that you\'ve failed ' +
'to create a plot before adding frames. For more details, see ' +
'https://plot.ly/javascript/animations/'
);
}

var i, frame, j, idx;
Expand Down
16 changes: 14 additions & 2 deletions test/jasmine/tests/animate_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,26 @@ describe('Test animate API', function() {
destroyGraphDiv();
});

it('throws an error if gd is not a graph', function() {
it('throws an error on addFrames if gd is not a graph', function() {
var gd2 = document.createElement('div');
gd2.id = 'invalidgd';
document.body.appendChild(gd2);

expect(function() {
Plotly.addFrames(gd2, [{}]);
}).toThrow(new Error('This element is not a Plotly plot: [object HTMLDivElement]'));
}).toThrow(new Error('This element is not a Plotly plot: [object HTMLDivElement]. It\'s likely that you\'ve failed to create a plot before adding frames. For more details, see https://plot.ly/javascript/animations/'));

document.body.removeChild(gd);
});

it('throws an error on animate if gd is not a graph', function() {
var gd2 = document.createElement('div');
gd2.id = 'invalidgd';
document.body.appendChild(gd2);

expect(function() {
Plotly.animate(gd2, {data: [{}]});
}).toThrow(new Error('This element is not a Plotly plot: [object HTMLDivElement]. It\'s likely that you\'ve failed to create a plot before animating it. For more details, see https://plot.ly/javascript/animations/'));

document.body.removeChild(gd);
});
Expand Down