@@ -5,11 +5,35 @@ var _ = require('lodash');
55
66/**
77 * Manipulates grid data
8- *
98 * @namespace
9+ *
1010 */
1111var 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 : / ^ ( m a i n G r i d | g r i d ) $ / ,
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+
180216window . Manipulator = Manipulator ;
181217module . exports = Manipulator ;
0 commit comments