1 | ;
|
2 |
|
3 | var format = require('util').format;
|
4 |
|
5 | /**
|
6 | * Factory functions to create throwable error objects
|
7 | * @module Errors
|
8 | */
|
9 |
|
10 | /**
|
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`.
|
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 | };
|
71 |
|
72 | /**
|
73 | * Creates an error object to be thrown when no files to be tested could be found using specified pattern.
|
74 | *
|
75 | * @public
|
76 | * @param {string} message - Error message to be displayed.
|
77 | * @param {string} pattern - User-specified argument value.
|
78 | * @returns {Error} instance detailing the error condition
|
79 | */
|
80 | function createNoFilesMatchPatternError(message, pattern) {
|
81 | var err = new Error(message);
|
82 | err.code = constants.NO_FILES_MATCH_PATTERN;
|
83 | err.pattern = pattern;
|
84 | return err;
|
85 | }
|
86 |
|
87 | /**
|
88 | * Creates an error object to be thrown when the reporter specified in the options was not found.
|
89 | *
|
90 | * @public
|
91 | * @param {string} message - Error message to be displayed.
|
92 | * @param {string} reporter - User-specified reporter value.
|
93 | * @returns {Error} instance detailing the error condition
|
94 | */
|
95 | function createInvalidReporterError(message, reporter) {
|
96 | var err = new TypeError(message);
|
97 | err.code = constants.INVALID_REPORTER;
|
98 | err.reporter = reporter;
|
99 | return err;
|
100 | }
|
101 |
|
102 | /**
|
103 | * Creates an error object to be thrown when the interface specified in the options was not found.
|
104 | *
|
105 | * @public
|
106 | * @param {string} message - Error message to be displayed.
|
107 | * @param {string} ui - User-specified interface value.
|
108 | * @returns {Error} instance detailing the error condition
|
109 | */
|
110 | function createInvalidInterfaceError(message, ui) {
|
111 | var err = new Error(message);
|
112 | err.code = constants.INVALID_INTERFACE;
|
113 | err.interface = ui;
|
114 | return err;
|
115 | }
|
116 |
|
117 | /**
|
118 | * Creates an error object to be thrown when a behavior, option, or parameter is unsupported.
|
119 | *
|
120 | * @public
|
121 | * @param {string} message - Error message to be displayed.
|
122 | * @returns {Error} instance detailing the error condition
|
123 | */
|
124 | function createUnsupportedError(message) {
|
125 | var err = new Error(message);
|
126 | err.code = constants.UNSUPPORTED;
|
127 | return err;
|
128 | }
|
129 |
|
130 | /**
|
131 | * Creates an error object to be thrown when an argument is missing.
|
132 | *
|
133 | * @public
|
134 | * @param {string} message - Error message to be displayed.
|
135 | * @param {string} argument - Argument name.
|
136 | * @param {string} expected - Expected argument datatype.
|
137 | * @returns {Error} instance detailing the error condition
|
138 | */
|
139 | function createMissingArgumentError(message, argument, expected) {
|
140 | return createInvalidArgumentTypeError(message, argument, expected);
|
141 | }
|
142 |
|
143 | /**
|
144 | * Creates an error object to be thrown when an argument did not use the supported type
|
145 | *
|
146 | * @public
|
147 | * @param {string} message - Error message to be displayed.
|
148 | * @param {string} argument - Argument name.
|
149 | * @param {string} expected - Expected argument datatype.
|
150 | * @returns {Error} instance detailing the error condition
|
151 | */
|
152 | function createInvalidArgumentTypeError(message, argument, expected) {
|
153 | var err = new TypeError(message);
|
154 | err.code = constants.INVALID_ARG_TYPE;
|
155 | err.argument = argument;
|
156 | err.expected = expected;
|
157 | err.actual = typeof argument;
|
158 | return err;
|
159 | }
|
160 |
|
161 | /**
|
162 | * Creates an error object to be thrown when an argument did not use the supported value
|
163 | *
|
164 | * @public
|
165 | * @param {string} message - Error message to be displayed.
|
166 | * @param {string} argument - Argument name.
|
167 | * @param {string} value - Argument value.
|
168 | * @param {string} [reason] - Why value is invalid.
|
169 | * @returns {Error} instance detailing the error condition
|
170 | */
|
171 | function createInvalidArgumentValueError(message, argument, value, reason) {
|
172 | var err = new TypeError(message);
|
173 | err.code = constants.INVALID_ARG_VALUE;
|
174 | err.argument = argument;
|
175 | err.value = value;
|
176 | err.reason = typeof reason !== 'undefined' ? reason : 'is invalid';
|
177 | return err;
|
178 | }
|
179 |
|
180 | /**
|
181 | * Creates an error object to be thrown when an exception was caught, but the `Error` is falsy or undefined.
|
182 | *
|
183 | * @public
|
184 | * @param {string} message - Error message to be displayed.
|
185 | * @returns {Error} instance detailing the error condition
|
186 | */
|
187 | function createInvalidExceptionError(message, value) {
|
188 | var err = new Error(message);
|
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;
|
205 | err.valueType = typeof value;
|
206 | err.value = value;
|
207 | return err;
|
208 | }
|
209 |
|
210 | /**
|
211 | * Dynamically creates a plugin-type-specific error based on plugin type
|
212 | * @param {string} message - Error message
|
213 | * @param {"reporter"|"interface"} pluginType - Plugin type. Future: expand as needed
|
214 | * @param {string} [pluginId] - Name/path of plugin, if any
|
215 | * @throws When `pluginType` is not known
|
216 | * @public
|
217 | * @returns {Error}
|
218 | */
|
219 | function createInvalidPluginError(message, pluginType, pluginId) {
|
220 | switch (pluginType) {
|
221 | case 'reporter':
|
222 | return createInvalidReporterError(message, pluginId);
|
223 | case 'interface':
|
224 | return createInvalidInterfaceError(message, pluginId);
|
225 | default:
|
226 | throw new Error('unknown pluginType "' + pluginType + '"');
|
227 | }
|
228 | }
|
229 |
|
230 | /**
|
231 | * Creates an error object to be thrown when a mocha object's `run` method is executed while it is already disposed.
|
232 | * @param {string} message The error message to be displayed.
|
233 | * @param {boolean} cleanReferencesAfterRun the value of `cleanReferencesAfterRun`
|
234 | * @param {Mocha} instance the mocha instance that throw this error
|
235 | */
|
236 | function createMochaInstanceAlreadyDisposedError(
|
237 | message,
|
238 | cleanReferencesAfterRun,
|
239 | instance
|
240 | ) {
|
241 | var err = new Error(message);
|
242 | err.code = constants.INSTANCE_ALREADY_DISPOSED;
|
243 | err.cleanReferencesAfterRun = cleanReferencesAfterRun;
|
244 | err.instance = instance;
|
245 | return err;
|
246 | }
|
247 |
|
248 | /**
|
249 | * Creates an error object to be thrown when a mocha object's `run` method is called while a test run is in progress.
|
250 | * @param {string} message The error message to be displayed.
|
251 | */
|
252 | function createMochaInstanceAlreadyRunningError(message, instance) {
|
253 | var err = new Error(message);
|
254 | err.code = constants.INSTANCE_ALREADY_RUNNING;
|
255 | err.instance = instance;
|
256 | return err;
|
257 | }
|
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 |
|
296 | module.exports = {
|
297 | createInvalidArgumentTypeError: createInvalidArgumentTypeError,
|
298 | createInvalidArgumentValueError: createInvalidArgumentValueError,
|
299 | createInvalidExceptionError: createInvalidExceptionError,
|
300 | createInvalidInterfaceError: createInvalidInterfaceError,
|
301 | createInvalidReporterError: createInvalidReporterError,
|
302 | createMissingArgumentError: createMissingArgumentError,
|
303 | createNoFilesMatchPatternError: createNoFilesMatchPatternError,
|
304 | createUnsupportedError: createUnsupportedError,
|
305 | createInvalidPluginError: createInvalidPluginError,
|
306 | createMochaInstanceAlreadyDisposedError: createMochaInstanceAlreadyDisposedError,
|
307 | createMochaInstanceAlreadyRunningError: createMochaInstanceAlreadyRunningError,
|
308 | createFatalError: createFatalError,
|
309 | createMultipleDoneError: createMultipleDoneError,
|
310 | constants: constants
|
311 | };
|