Skip to content

Commit dd8e68c

Browse files
committed
Use real exceptions when type is invalid
See davidshimjs/jaguarjs-jsdoc#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
1 parent b4f4d5d commit dd8e68c

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

app/Grid/Manipulator.js

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,35 @@ var _ = require('lodash');
55

66
/**
77
* Manipulates grid data
8-
*
98
* @namespace
9+
*
1010
*/
1111
var Manipulator = {
1212

13+
/**
14+
* Exceptions for the Manipulator module
15+
* @namespace
16+
*
17+
*/
18+
Exceptions: {
19+
/**
20+
* Exception raised when a type is invalid
21+
* This is a subclass of "Error"
22+
* @class
23+
*
24+
* @param {string} [message] - The raised message
25+
*
26+
* @returns {} - Return an "InvalidType" object, which is a subclass of "Error"
27+
*
28+
* @property {string} name - The name of the exception: "InvalidType"
29+
* @property {string} message - The message passed when the exception was raised, or a default value
30+
*/
31+
InvalidType: function(message) {
32+
this.name = 'InvalidType';
33+
this.message = message || 'Invalid type detected';
34+
},
35+
},
36+
1337
// Nodes types that can directly accept rows
1438
reGrid: /^(mainGrid|grid)$/,
1539

@@ -131,10 +155,12 @@ var Manipulator = {
131155
* If not given, a new empty "content" node will be created.
132156
*
133157
* @returns {XML} - The added cell (XML), with the type and a content.
158+
*
159+
* @throws {module:Grid~Manipulator.Exceptions.InvalidType} If the given "type" is not "grid" or "module"
134160
*/
135161
addCell: function(row, type, contentNode) {
136162
if (!this.reType.test(type)) {
137-
throw "Invalid type <" + type + ">. Should be 'grid' or 'module'";
163+
throw new this.Exceptions.InvalidType("Cannot add cell of type <" + type + ">. Should be <grid> or <module>");
138164
}
139165
var cell = row.ownerDocument.createElement('cells');
140166
cell.setAttribute('type', type);
@@ -153,9 +179,14 @@ var Manipulator = {
153179
* @param {XML} node - The JSON grid node to clean
154180
*
155181
* @returns {} - Returns nothing
182+
*
183+
* @throws {module:Grid~Manipulator.Exceptions.InvalidType} If the type of the given node is not "grid"
156184
*/
157185
cleanNode: function(node) {
158-
if (node.getAttribute('type') != 'grid') { return }
186+
var nodeType = node.getAttribute('type');
187+
if (nodeType != 'grid') {
188+
throw new this.Exceptions.InvalidType("Cannot clean node of type <" + nodeType + ">. Should be <grid>");
189+
}
159190

160191
var contentNode = node.querySelector(':scope > content');
161192
var rows = contentNode.querySelectorAll(':scope > rows');
@@ -177,5 +208,10 @@ var Manipulator = {
177208
}
178209
};
179210

211+
// Exceptions must be based on the Error class
212+
Manipulator.Exceptions.InvalidType.prototype = new Error();
213+
Manipulator.Exceptions.InvalidType.prototype.constructor = Manipulator.Exceptions.InvalidType;
214+
215+
180216
window.Manipulator = Manipulator;
181217
module.exports = Manipulator;

specs/Grid/Manipulator-spec.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,11 @@ describe("Manipulator", function() {
240240
'</grid>';
241241
expect(Manipulator.XMLGridToXMLString(grid)).toEqual(expected);
242242

243+
// shouldn't be able to add cell with an invalid type
244+
expect(function() {
245+
Manipulator.addCell(row, 'foo')
246+
}).toThrowError(Manipulator.Exceptions.InvalidType, "Cannot add cell of type <foo>. Should be <grid> or <module>");
247+
243248
});
244249

245250
it("should create a full grid", function() {
@@ -330,8 +335,11 @@ describe("Manipulator", function() {
330335
var cellContent = cell.querySelector(':scope > content');
331336
cellContent.setAttribute('foo', 'bar');
332337

333-
// we should not update the main grid
334-
Manipulator.cleanNode(grid.firstChild);
338+
// we cannot update the main grid
339+
expect(function() {
340+
Manipulator.cleanNode(grid.firstChild);
341+
}).toThrowError(Manipulator.Exceptions.InvalidType, "Cannot clean node of type <mainGrid>. Should be <grid>");
342+
335343
var expected = {
336344
_name: 'test',
337345
_space: '5px',

0 commit comments

Comments
 (0)