Skip to content
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

Handles situations where the vislib is passed a data object with no data #2300

Merged
merged 7 commits into from
Jan 6, 2015
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
10 changes: 10 additions & 0 deletions src/kibana/components/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,5 +214,15 @@ define(function (require) {
};
inherits(errors.NotEnoughData, KbnError);

/**
* error thrown when no results are returned from an elasticsearch query
*/
errors.NoResults = function NoResults() {
KbnError.call(this,
'No results found',
errors.NoResults);
};
inherits(errors.NoResults, KbnError);

return errors;
});
2 changes: 2 additions & 0 deletions src/kibana/components/vislib/lib/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ define(function (require) {
type = 'series';
} else if (obj.slices) {
type = 'slices';
} else if (obj.geoJSON) {
type = 'goeJSON';
}
});

Expand Down
38 changes: 34 additions & 4 deletions src/kibana/components/vislib/lib/handler/handler.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
define(function (require) {
return function HandlerBaseClass(d3, Private) {
var _ = require('lodash');
var errors = require('errors');

var Data = Private(require('components/vislib/lib/data'));
var Layout = Private(require('components/vislib/lib/layout/layout'));
Expand All @@ -20,6 +21,7 @@ define(function (require) {
}

this.data = opts.data || new Data(vis.data, vis._attr);

this.vis = vis;
this.el = vis.el;
this.ChartClass = vis.ChartClass;
Expand Down Expand Up @@ -49,6 +51,21 @@ define(function (require) {
], Boolean);
}

/**
* Validates whether data is actually present in the data object
* used to render the Vis. Throws a no results error if data is not
* present.
*
* @private
*/
Handler.prototype._validateData = function () {
var dataType = this.data.type;

if (!dataType) {
throw new errors.NoResults();
}
};

/**
* Renders the constructors that create the visualization,
* including the chart constructor
Expand All @@ -60,6 +77,7 @@ define(function (require) {
var self = this;
var charts = this.charts = [];

this._validateData();
this.renderArray.forEach(function (property) {
if (typeof property.render === 'function') {
property.render();
Expand Down Expand Up @@ -153,13 +171,25 @@ define(function (require) {
Handler.prototype.error = function (message) {
this.removeAll(this.el);

return d3.select(this.el)
var div = d3.select(this.el)
.append('div')
// class name needs `chart` in it for the polling checkSize function
// to continuously call render on resize
.attr('class', 'visualize-error chart error')
.append('h4')
.text(message);
.attr('class', 'visualize-error chart error');

if (message === 'No results found') {
div.append('div')
.attr('class', 'text-center visualize-error visualize-chart ng-scope')
.append('div').attr('class', 'item top')
.append('div').attr('class', 'item')
.append('h2').html('<i class="fa fa-meh-o"></i>')
.append('h4').text(message);

div.append('div').attr('class', 'item bottom');
return div;
}

return div.append('h4').text(message);
};

/**
Expand Down
3 changes: 2 additions & 1 deletion src/kibana/components/vislib/vis.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ define(function (require) {
// Because we have to wait for the DOM element to initialize, we do not
// want to throw an error when the DOM `el` is zero
if (error instanceof errors.ContainerTooSmall ||
error instanceof errors.NotEnoughData) {
error instanceof errors.NotEnoughData ||
error instanceof errors.NoResults) {
this.handler.error(error.message);
} else {
throw error;
Expand Down