From 06383669beb1092922cd162041910c331c7a6955 Mon Sep 17 00:00:00 2001 From: etimberg Date: Sat, 25 Mar 2017 19:34:55 -0400 Subject: [PATCH] Adds a better error message when the chart type is incorrect. Previously the user got a message about `type` being undefined. This gives something that's easier to understand and debug. --- src/core/core.controller.js | 7 ++++++- test/specs/core.controller.tests.js | 26 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/core/core.controller.js b/src/core/core.controller.js index 1090aa9b8..71eb3bb62 100644 --- a/src/core/core.controller.js +++ b/src/core/core.controller.js @@ -293,7 +293,12 @@ module.exports = function(Chart) { if (meta.controller) { meta.controller.updateIndex(datasetIndex); } else { - meta.controller = new Chart.controllers[meta.type](me, datasetIndex); + var ControllerClass = Chart.controllers[meta.type]; + if (ControllerClass === undefined) { + throw new Error('"' + meta.type + '" is not a chart type.'); + } + + meta.controller = new ControllerClass(me, datasetIndex); newControllers.push(meta.controller); } }, me); diff --git a/test/specs/core.controller.tests.js b/test/specs/core.controller.tests.js index 18caee8bd..1849da8c3 100644 --- a/test/specs/core.controller.tests.js +++ b/test/specs/core.controller.tests.js @@ -144,6 +144,32 @@ describe('Chart', function() { expect(scaleOptions.xAxes[0].position).toBe('bottom'); expect(scaleOptions.yAxes[0].position).toBe('left'); }); + + it('should throw an error if the chart type is incorrect', function() { + function createChart() { + acquireChart({ + type: 'area', + data: { + datasets: [{ + label: 'first', + data: [10, 20] + }], + labels: ['0', '1'], + }, + options: { + scales: { + xAxes: [{ + position: 'left', + }], + yAxes: [{ + position: 'bottom' + }] + } + } + }); + } + expect(createChart).toThrow(new Error('"area" is not a chart type.')); + }); }); describe('config.options.responsive: false', function() { -- 2.47.3