Skip to content

Commit

Permalink
Use real exceptions when type is invalid
Browse files Browse the repository at this point in the history
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
  • Loading branch information
twidi committed Nov 24, 2014
1 parent b4f4d5d commit dd8e68c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
42 changes: 39 additions & 3 deletions app/Grid/Manipulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)$/,

Expand Down Expand Up @@ -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 <grid> or <module>");
}
var cell = row.ownerDocument.createElement('cells');
cell.setAttribute('type', type);
Expand All @@ -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 <grid>");
}

var contentNode = node.querySelector(':scope > content');
var rows = contentNode.querySelectorAll(':scope > rows');
Expand All @@ -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;
12 changes: 10 additions & 2 deletions specs/Grid/Manipulator-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@ describe("Manipulator", function() {
'</grid>';
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 <foo>. Should be <grid> or <module>");

});

it("should create a full grid", function() {
Expand Down Expand Up @@ -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 <mainGrid>. Should be <grid>");

var expected = {
_name: 'test',
_space: '5px',
Expand Down

0 comments on commit dd8e68c

Please sign in to comment.