@@ -5,11 +5,35 @@ var _ = require('lodash');
5
5
6
6
/**
7
7
* Manipulates grid data
8
- *
9
8
* @namespace
9
+ *
10
10
*/
11
11
var Manipulator = {
12
12
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
+
13
37
// Nodes types that can directly accept rows
14
38
reGrid : / ^ ( m a i n G r i d | g r i d ) $ / ,
15
39
@@ -131,10 +155,12 @@ var Manipulator = {
131
155
* If not given, a new empty "content" node will be created.
132
156
*
133
157
* @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"
134
160
*/
135
161
addCell : function ( row , type , contentNode ) {
136
162
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>" ) ;
138
164
}
139
165
var cell = row . ownerDocument . createElement ( 'cells' ) ;
140
166
cell . setAttribute ( 'type' , type ) ;
@@ -153,9 +179,14 @@ var Manipulator = {
153
179
* @param {XML } node - The JSON grid node to clean
154
180
*
155
181
* @returns { } - Returns nothing
182
+ *
183
+ * @throws {module:Grid~Manipulator.Exceptions.InvalidType } If the type of the given node is not "grid"
156
184
*/
157
185
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
+ }
159
190
160
191
var contentNode = node . querySelector ( ':scope > content' ) ;
161
192
var rows = contentNode . querySelectorAll ( ':scope > rows' ) ;
@@ -177,5 +208,10 @@ var Manipulator = {
177
208
}
178
209
} ;
179
210
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
+
180
216
window . Manipulator = Manipulator ;
181
217
module . exports = Manipulator ;
0 commit comments