1
1
'use strict' ;
2
+
3
+ var format = require ( 'util' ) . format ;
4
+
2
5
/**
6
+ * Factory functions to create throwable error objects
3
7
* @module Errors
4
8
*/
9
+
5
10
/**
6
- * Factory functions to create throwable error objects
11
+ * When Mocha throw exceptions (or otherwise errors), it attempts to assign a
12
+ * `code` property to the `Error` object, for easier handling. These are the
13
+ * potential values of `code`.
7
14
*/
15
+ var constants = {
16
+ /**
17
+ * An unrecoverable error.
18
+ */
19
+ FATAL : 'ERR_MOCHA_FATAL' ,
20
+
21
+ /**
22
+ * The type of an argument to a function call is invalid
23
+ */
24
+ INVALID_ARG_TYPE : 'ERR_MOCHA_INVALID_ARG_TYPE' ,
25
+
26
+ /**
27
+ * The value of an argument to a function call is invalid
28
+ */
29
+ INVALID_ARG_VALUE : 'ERR_MOCHA_INVALID_ARG_VALUE' ,
30
+
31
+ /**
32
+ * Something was thrown, but it wasn't an `Error`
33
+ */
34
+ INVALID_EXCEPTION : 'ERR_MOCHA_INVALID_EXCEPTION' ,
35
+
36
+ /**
37
+ * An interface (e.g., `Mocha.interfaces`) is unknown or invalid
38
+ */
39
+ INVALID_INTERFACE : 'ERR_MOCHA_INVALID_INTERFACE' ,
40
+
41
+ /**
42
+ * A reporter (.e.g, `Mocha.reporters`) is unknown or invalid
43
+ */
44
+ INVALID_REPORTER : 'ERR_MOCHA_INVALID_REPORTER' ,
45
+
46
+ /**
47
+ * `done()` was called twice in a `Test` or `Hook` callback
48
+ */
49
+ MULTIPLE_DONE : 'ERR_MOCHA_MULTIPLE_DONE' ,
50
+
51
+ /**
52
+ * No files matched the pattern provided by the user
53
+ */
54
+ NO_FILES_MATCH_PATTERN : 'ERR_MOCHA_NO_FILES_MATCH_PATTERN' ,
55
+
56
+ /**
57
+ * Known, but unsupported behavior of some kind
58
+ */
59
+ UNSUPPORTED : 'ERR_MOCHA_UNSUPPORTED' ,
60
+
61
+ /**
62
+ * Invalid state transition occuring in `Mocha` instance
63
+ */
64
+ INSTANCE_ALREADY_RUNNING : 'ERR_MOCHA_INSTANCE_ALREADY_RUNNING' ,
65
+
66
+ /**
67
+ * Invalid state transition occuring in `Mocha` instance
68
+ */
69
+ INSTANCE_ALREADY_DISPOSED : 'ERR_MOCHA_INSTANCE_ALREADY_DISPOSED'
70
+ } ;
8
71
9
72
/**
10
73
* Creates an error object to be thrown when no files to be tested could be found using specified pattern.
16
79
*/
17
80
function createNoFilesMatchPatternError ( message , pattern ) {
18
81
var err = new Error ( message ) ;
19
- err . code = 'ERR_MOCHA_NO_FILES_MATCH_PATTERN' ;
82
+ err . code = constants . NO_FILES_MATCH_PATTERN ;
20
83
err . pattern = pattern ;
21
84
return err ;
22
85
}
@@ -31,7 +94,7 @@ function createNoFilesMatchPatternError(message, pattern) {
31
94
*/
32
95
function createInvalidReporterError ( message , reporter ) {
33
96
var err = new TypeError ( message ) ;
34
- err . code = 'ERR_MOCHA_INVALID_REPORTER' ;
97
+ err . code = constants . INVALID_REPORTER ;
35
98
err . reporter = reporter ;
36
99
return err ;
37
100
}
@@ -46,7 +109,7 @@ function createInvalidReporterError(message, reporter) {
46
109
*/
47
110
function createInvalidInterfaceError ( message , ui ) {
48
111
var err = new Error ( message ) ;
49
- err . code = 'ERR_MOCHA_INVALID_INTERFACE' ;
112
+ err . code = constants . INVALID_INTERFACE ;
50
113
err . interface = ui ;
51
114
return err ;
52
115
}
@@ -60,7 +123,7 @@ function createInvalidInterfaceError(message, ui) {
60
123
*/
61
124
function createUnsupportedError ( message ) {
62
125
var err = new Error ( message ) ;
63
- err . code = 'ERR_MOCHA_UNSUPPORTED' ;
126
+ err . code = constants . UNSUPPORTED ;
64
127
return err ;
65
128
}
66
129
@@ -88,7 +151,7 @@ function createMissingArgumentError(message, argument, expected) {
88
151
*/
89
152
function createInvalidArgumentTypeError ( message , argument , expected ) {
90
153
var err = new TypeError ( message ) ;
91
- err . code = 'ERR_MOCHA_INVALID_ARG_TYPE' ;
154
+ err . code = constants . INVALID_ARG_TYPE ;
92
155
err . argument = argument ;
93
156
err . expected = expected ;
94
157
err . actual = typeof argument ;
@@ -107,7 +170,7 @@ function createInvalidArgumentTypeError(message, argument, expected) {
107
170
*/
108
171
function createInvalidArgumentValueError ( message , argument , value , reason ) {
109
172
var err = new TypeError ( message ) ;
110
- err . code = 'ERR_MOCHA_INVALID_ARG_VALUE' ;
173
+ err . code = constants . INVALID_ARG_VALUE ;
111
174
err . argument = argument ;
112
175
err . value = value ;
113
176
err . reason = typeof reason !== 'undefined' ? reason : 'is invalid' ;
@@ -123,7 +186,22 @@ function createInvalidArgumentValueError(message, argument, value, reason) {
123
186
*/
124
187
function createInvalidExceptionError ( message , value ) {
125
188
var err = new Error ( message ) ;
126
- err . code = 'ERR_MOCHA_INVALID_EXCEPTION' ;
189
+ err . code = constants . INVALID_EXCEPTION ;
190
+ err . valueType = typeof value ;
191
+ err . value = value ;
192
+ return err ;
193
+ }
194
+
195
+ /**
196
+ * Creates an error object to be thrown when an unrecoverable error occurs.
197
+ *
198
+ * @public
199
+ * @param {string } message - Error message to be displayed.
200
+ * @returns {Error } instance detailing the error condition
201
+ */
202
+ function createFatalError ( message , value ) {
203
+ var err = new Error ( message ) ;
204
+ err . code = constants . FATAL ;
127
205
err . valueType = typeof value ;
128
206
err . value = value ;
129
207
return err ;
@@ -161,7 +239,7 @@ function createMochaInstanceAlreadyDisposedError(
161
239
instance
162
240
) {
163
241
var err = new Error ( message ) ;
164
- err . code = 'ERR_MOCHA_INSTANCE_ALREADY_DISPOSED' ;
242
+ err . code = constants . INSTANCE_ALREADY_DISPOSED ;
165
243
err . cleanReferencesAfterRun = cleanReferencesAfterRun ;
166
244
err . instance = instance ;
167
245
return err ;
@@ -173,11 +251,48 @@ function createMochaInstanceAlreadyDisposedError(
173
251
*/
174
252
function createMochaInstanceAlreadyRunningError ( message , instance ) {
175
253
var err = new Error ( message ) ;
176
- err . code = 'ERR_MOCHA_INSTANCE_ALREADY_RUNNING' ;
254
+ err . code = constants . INSTANCE_ALREADY_RUNNING ;
177
255
err . instance = instance ;
178
256
return err ;
179
257
}
180
258
259
+ /*
260
+ * Creates an error object to be thrown when done() is called multiple times in a test
261
+ *
262
+ * @public
263
+ * @param {Runnable } runnable - Original runnable
264
+ * @param {Error } [originalErr] - Original error, if any
265
+ * @returns {Error } instance detailing the error condition
266
+ */
267
+ function createMultipleDoneError ( runnable , originalErr ) {
268
+ var title ;
269
+ try {
270
+ title = format ( '<%s>' , runnable . fullTitle ( ) ) ;
271
+ if ( runnable . parent . root ) {
272
+ title += ' (of root suite)' ;
273
+ }
274
+ } catch ( ignored ) {
275
+ title = format ( '<%s> (of unknown suite)' , runnable . title ) ;
276
+ }
277
+ var message = format (
278
+ 'done() called multiple times in %s %s' ,
279
+ runnable . type ? runnable . type : 'unknown runnable' ,
280
+ title
281
+ ) ;
282
+ if ( runnable . file ) {
283
+ message += format ( ' of file %s' , runnable . file ) ;
284
+ }
285
+ if ( originalErr ) {
286
+ message += format ( '; in addition, done() received error: %s' , originalErr ) ;
287
+ }
288
+
289
+ var err = new Error ( message ) ;
290
+ err . code = constants . MULTIPLE_DONE ;
291
+ err . valueType = typeof originalErr ;
292
+ err . value = originalErr ;
293
+ return err ;
294
+ }
295
+
181
296
module . exports = {
182
297
createInvalidArgumentTypeError : createInvalidArgumentTypeError ,
183
298
createInvalidArgumentValueError : createInvalidArgumentValueError ,
@@ -189,5 +304,8 @@ module.exports = {
189
304
createUnsupportedError : createUnsupportedError ,
190
305
createInvalidPluginError : createInvalidPluginError ,
191
306
createMochaInstanceAlreadyDisposedError : createMochaInstanceAlreadyDisposedError ,
192
- createMochaInstanceAlreadyRunningError : createMochaInstanceAlreadyRunningError
307
+ createMochaInstanceAlreadyRunningError : createMochaInstanceAlreadyRunningError ,
308
+ createFatalError : createFatalError ,
309
+ createMultipleDoneError : createMultipleDoneError ,
310
+ constants : constants
193
311
} ;
0 commit comments