From dd8e68c0ca329bde4036c09cf6c598c3cae45988 Mon Sep 17 00:00:00 2001 From: twidi Date: Mon, 24 Nov 2014 21:38:04 +0100 Subject: [PATCH] Use real exceptions when type is invalid See https://github.com/davidshimjs/jaguarjs-jsdoc/pull/31 for an update of the jsdoc template that don't currently manage exceptions correctly I currently use https://github.com/twidi/jaguarjs-jsdoc/tree/twidi- current which integrates this PR and some others for the original repository --- app/Grid/Manipulator.js | 42 +++++++++++++++++++++++++++++++--- specs/Grid/Manipulator-spec.js | 12 ++++++++-- 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/app/Grid/Manipulator.js b/app/Grid/Manipulator.js index d9c1d22..1a34397 100644 --- a/app/Grid/Manipulator.js +++ b/app/Grid/Manipulator.js @@ -5,11 +5,35 @@ var _ = require('lodash'); /** * Manipulates grid data - * * @namespace + * */ var Manipulator = { + /** + * Exceptions for the Manipulator module + * @namespace + * + */ + Exceptions: { + /** + * Exception raised when a type is invalid + * This is a subclass of "Error" + * @class + * + * @param {string} [message] - The raised message + * + * @returns {} - Return an "InvalidType" object, which is a subclass of "Error" + * + * @property {string} name - The name of the exception: "InvalidType" + * @property {string} message - The message passed when the exception was raised, or a default value + */ + InvalidType: function(message) { + this.name = 'InvalidType'; + this.message = message || 'Invalid type detected'; + }, + }, + // Nodes types that can directly accept rows reGrid: /^(mainGrid|grid)$/, @@ -131,10 +155,12 @@ var Manipulator = { * If not given, a new empty "content" node will be created. * * @returns {XML} - The added cell (XML), with the type and a content. + * + * @throws {module:Grid~Manipulator.Exceptions.InvalidType} If the given "type" is not "grid" or "module" */ addCell: function(row, type, contentNode) { if (!this.reType.test(type)) { - throw "Invalid type <" + type + ">. Should be 'grid' or 'module'"; + throw new this.Exceptions.InvalidType("Cannot add cell of type <" + type + ">. Should be or "); } var cell = row.ownerDocument.createElement('cells'); cell.setAttribute('type', type); @@ -153,9 +179,14 @@ var Manipulator = { * @param {XML} node - The JSON grid node to clean * * @returns {} - Returns nothing + * + * @throws {module:Grid~Manipulator.Exceptions.InvalidType} If the type of the given node is not "grid" */ cleanNode: function(node) { - if (node.getAttribute('type') != 'grid') { return } + var nodeType = node.getAttribute('type'); + if (nodeType != 'grid') { + throw new this.Exceptions.InvalidType("Cannot clean node of type <" + nodeType + ">. Should be "); + } var contentNode = node.querySelector(':scope > content'); var rows = contentNode.querySelectorAll(':scope > rows'); @@ -177,5 +208,10 @@ var Manipulator = { } }; +// Exceptions must be based on the Error class +Manipulator.Exceptions.InvalidType.prototype = new Error(); +Manipulator.Exceptions.InvalidType.prototype.constructor = Manipulator.Exceptions.InvalidType; + + window.Manipulator = Manipulator; module.exports = Manipulator; \ No newline at end of file diff --git a/specs/Grid/Manipulator-spec.js b/specs/Grid/Manipulator-spec.js index ac60383..5d9c58d 100644 --- a/specs/Grid/Manipulator-spec.js +++ b/specs/Grid/Manipulator-spec.js @@ -240,6 +240,11 @@ describe("Manipulator", function() { ''; expect(Manipulator.XMLGridToXMLString(grid)).toEqual(expected); + // shouldn't be able to add cell with an invalid type + expect(function() { + Manipulator.addCell(row, 'foo') + }).toThrowError(Manipulator.Exceptions.InvalidType, "Cannot add cell of type . Should be or "); + }); it("should create a full grid", function() { @@ -330,8 +335,11 @@ describe("Manipulator", function() { var cellContent = cell.querySelector(':scope > content'); cellContent.setAttribute('foo', 'bar'); - // we should not update the main grid - Manipulator.cleanNode(grid.firstChild); + // we cannot update the main grid + expect(function() { + Manipulator.cleanNode(grid.firstChild); + }).toThrowError(Manipulator.Exceptions.InvalidType, "Cannot clean node of type . Should be "); + var expected = { _name: 'test', _space: '5px',