Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 1 | 'use strict'; |
Peter Marshall | 0b95ea1 | 2020-07-02 18:50:04 +0200 | [diff] [blame] | 2 | |
Peter Marshall | 4e161df | 2020-11-10 13:29:38 +0100 | [diff] [blame] | 3 | const {format} = require('util'); |
Peter Marshall | 0b95ea1 | 2020-07-02 18:50:04 +0200 | [diff] [blame] | 4 | |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 5 | /** |
Peter Marshall | 4e161df | 2020-11-10 13:29:38 +0100 | [diff] [blame] | 6 | * Contains error codes, factory functions to create throwable error objects, |
| 7 | * and warning/deprecation functions. |
| 8 | * @module |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | /** |
Peter Marshall | 4e161df | 2020-11-10 13:29:38 +0100 | [diff] [blame] | 12 | * process.emitWarning or a polyfill |
| 13 | * @see https://nodejs.org/api/process.html#process_process_emitwarning_warning_options |
| 14 | * @ignore |
| 15 | */ |
| 16 | const emitWarning = (msg, type) => { |
| 17 | if (process.emitWarning) { |
| 18 | process.emitWarning(msg, type); |
| 19 | } else { |
Tim van der Lippe | 6d109a9 | 2021-02-16 16:00:32 +0000 | [diff] [blame^] | 20 | /* istanbul ignore next */ |
Peter Marshall | 4e161df | 2020-11-10 13:29:38 +0100 | [diff] [blame] | 21 | process.nextTick(function() { |
| 22 | console.warn(type + ': ' + msg); |
| 23 | }); |
| 24 | } |
| 25 | }; |
| 26 | |
| 27 | /** |
| 28 | * Show a deprecation warning. Each distinct message is only displayed once. |
| 29 | * Ignores empty messages. |
| 30 | * |
| 31 | * @param {string} [msg] - Warning to print |
| 32 | * @private |
| 33 | */ |
| 34 | const deprecate = msg => { |
| 35 | msg = String(msg); |
| 36 | if (msg && !deprecate.cache[msg]) { |
| 37 | deprecate.cache[msg] = true; |
| 38 | emitWarning(msg, 'DeprecationWarning'); |
| 39 | } |
| 40 | }; |
| 41 | deprecate.cache = {}; |
| 42 | |
| 43 | /** |
| 44 | * Show a generic warning. |
| 45 | * Ignores empty messages. |
| 46 | * |
| 47 | * @param {string} [msg] - Warning to print |
| 48 | * @private |
| 49 | */ |
| 50 | const warn = msg => { |
| 51 | if (msg) { |
| 52 | emitWarning(msg); |
| 53 | } |
| 54 | }; |
| 55 | |
| 56 | /** |
Tim van der Lippe | 6d109a9 | 2021-02-16 16:00:32 +0000 | [diff] [blame^] | 57 | * When Mocha throws exceptions (or rejects `Promise`s), it attempts to assign a `code` property to the `Error` object, for easier handling. These are the potential values of `code`. |
| 58 | * @public |
| 59 | * @namespace |
| 60 | * @memberof module:lib/errors |
Peter Marshall | 0b95ea1 | 2020-07-02 18:50:04 +0200 | [diff] [blame] | 61 | */ |
| 62 | var constants = { |
| 63 | /** |
| 64 | * An unrecoverable error. |
Tim van der Lippe | 6d109a9 | 2021-02-16 16:00:32 +0000 | [diff] [blame^] | 65 | * @constant |
| 66 | * @default |
Peter Marshall | 0b95ea1 | 2020-07-02 18:50:04 +0200 | [diff] [blame] | 67 | */ |
| 68 | FATAL: 'ERR_MOCHA_FATAL', |
| 69 | |
| 70 | /** |
| 71 | * The type of an argument to a function call is invalid |
Tim van der Lippe | 6d109a9 | 2021-02-16 16:00:32 +0000 | [diff] [blame^] | 72 | * @constant |
| 73 | * @default |
Peter Marshall | 0b95ea1 | 2020-07-02 18:50:04 +0200 | [diff] [blame] | 74 | */ |
| 75 | INVALID_ARG_TYPE: 'ERR_MOCHA_INVALID_ARG_TYPE', |
| 76 | |
| 77 | /** |
| 78 | * The value of an argument to a function call is invalid |
Tim van der Lippe | 6d109a9 | 2021-02-16 16:00:32 +0000 | [diff] [blame^] | 79 | * @constant |
| 80 | * @default |
Peter Marshall | 0b95ea1 | 2020-07-02 18:50:04 +0200 | [diff] [blame] | 81 | */ |
| 82 | INVALID_ARG_VALUE: 'ERR_MOCHA_INVALID_ARG_VALUE', |
| 83 | |
| 84 | /** |
| 85 | * Something was thrown, but it wasn't an `Error` |
Tim van der Lippe | 6d109a9 | 2021-02-16 16:00:32 +0000 | [diff] [blame^] | 86 | * @constant |
| 87 | * @default |
Peter Marshall | 0b95ea1 | 2020-07-02 18:50:04 +0200 | [diff] [blame] | 88 | */ |
| 89 | INVALID_EXCEPTION: 'ERR_MOCHA_INVALID_EXCEPTION', |
| 90 | |
| 91 | /** |
| 92 | * An interface (e.g., `Mocha.interfaces`) is unknown or invalid |
Tim van der Lippe | 6d109a9 | 2021-02-16 16:00:32 +0000 | [diff] [blame^] | 93 | * @constant |
| 94 | * @default |
Peter Marshall | 0b95ea1 | 2020-07-02 18:50:04 +0200 | [diff] [blame] | 95 | */ |
| 96 | INVALID_INTERFACE: 'ERR_MOCHA_INVALID_INTERFACE', |
| 97 | |
| 98 | /** |
| 99 | * A reporter (.e.g, `Mocha.reporters`) is unknown or invalid |
Tim van der Lippe | 6d109a9 | 2021-02-16 16:00:32 +0000 | [diff] [blame^] | 100 | * @constant |
| 101 | * @default |
Peter Marshall | 0b95ea1 | 2020-07-02 18:50:04 +0200 | [diff] [blame] | 102 | */ |
| 103 | INVALID_REPORTER: 'ERR_MOCHA_INVALID_REPORTER', |
| 104 | |
| 105 | /** |
| 106 | * `done()` was called twice in a `Test` or `Hook` callback |
Tim van der Lippe | 6d109a9 | 2021-02-16 16:00:32 +0000 | [diff] [blame^] | 107 | * @constant |
| 108 | * @default |
Peter Marshall | 0b95ea1 | 2020-07-02 18:50:04 +0200 | [diff] [blame] | 109 | */ |
| 110 | MULTIPLE_DONE: 'ERR_MOCHA_MULTIPLE_DONE', |
| 111 | |
| 112 | /** |
| 113 | * No files matched the pattern provided by the user |
Tim van der Lippe | 6d109a9 | 2021-02-16 16:00:32 +0000 | [diff] [blame^] | 114 | * @constant |
| 115 | * @default |
Peter Marshall | 0b95ea1 | 2020-07-02 18:50:04 +0200 | [diff] [blame] | 116 | */ |
| 117 | NO_FILES_MATCH_PATTERN: 'ERR_MOCHA_NO_FILES_MATCH_PATTERN', |
| 118 | |
| 119 | /** |
| 120 | * Known, but unsupported behavior of some kind |
Tim van der Lippe | 6d109a9 | 2021-02-16 16:00:32 +0000 | [diff] [blame^] | 121 | * @constant |
| 122 | * @default |
Peter Marshall | 0b95ea1 | 2020-07-02 18:50:04 +0200 | [diff] [blame] | 123 | */ |
| 124 | UNSUPPORTED: 'ERR_MOCHA_UNSUPPORTED', |
| 125 | |
| 126 | /** |
Peter Marshall | 4e161df | 2020-11-10 13:29:38 +0100 | [diff] [blame] | 127 | * Invalid state transition occurring in `Mocha` instance |
Tim van der Lippe | 6d109a9 | 2021-02-16 16:00:32 +0000 | [diff] [blame^] | 128 | * @constant |
| 129 | * @default |
Peter Marshall | 0b95ea1 | 2020-07-02 18:50:04 +0200 | [diff] [blame] | 130 | */ |
| 131 | INSTANCE_ALREADY_RUNNING: 'ERR_MOCHA_INSTANCE_ALREADY_RUNNING', |
| 132 | |
| 133 | /** |
Peter Marshall | 4e161df | 2020-11-10 13:29:38 +0100 | [diff] [blame] | 134 | * Invalid state transition occurring in `Mocha` instance |
Tim van der Lippe | 6d109a9 | 2021-02-16 16:00:32 +0000 | [diff] [blame^] | 135 | * @constant |
| 136 | * @default |
Peter Marshall | 0b95ea1 | 2020-07-02 18:50:04 +0200 | [diff] [blame] | 137 | */ |
| 138 | INSTANCE_ALREADY_DISPOSED: 'ERR_MOCHA_INSTANCE_ALREADY_DISPOSED', |
| 139 | |
| 140 | /** |
| 141 | * Use of `only()` w/ `--forbid-only` results in this error. |
Tim van der Lippe | 6d109a9 | 2021-02-16 16:00:32 +0000 | [diff] [blame^] | 142 | * @constant |
| 143 | * @default |
Peter Marshall | 0b95ea1 | 2020-07-02 18:50:04 +0200 | [diff] [blame] | 144 | */ |
Peter Marshall | 4e161df | 2020-11-10 13:29:38 +0100 | [diff] [blame] | 145 | FORBIDDEN_EXCLUSIVITY: 'ERR_MOCHA_FORBIDDEN_EXCLUSIVITY', |
| 146 | |
| 147 | /** |
| 148 | * To be thrown when a user-defined plugin implementation (e.g., `mochaHooks`) is invalid |
Tim van der Lippe | 6d109a9 | 2021-02-16 16:00:32 +0000 | [diff] [blame^] | 149 | * @constant |
| 150 | * @default |
Peter Marshall | 4e161df | 2020-11-10 13:29:38 +0100 | [diff] [blame] | 151 | */ |
| 152 | INVALID_PLUGIN_IMPLEMENTATION: 'ERR_MOCHA_INVALID_PLUGIN_IMPLEMENTATION', |
| 153 | |
| 154 | /** |
| 155 | * To be thrown when a builtin or third-party plugin definition (the _definition_ of `mochaHooks`) is invalid |
Tim van der Lippe | 6d109a9 | 2021-02-16 16:00:32 +0000 | [diff] [blame^] | 156 | * @constant |
| 157 | * @default |
Peter Marshall | 4e161df | 2020-11-10 13:29:38 +0100 | [diff] [blame] | 158 | */ |
Tim van der Lippe | 6d109a9 | 2021-02-16 16:00:32 +0000 | [diff] [blame^] | 159 | INVALID_PLUGIN_DEFINITION: 'ERR_MOCHA_INVALID_PLUGIN_DEFINITION', |
| 160 | |
| 161 | /** |
| 162 | * When a runnable exceeds its allowed run time. |
| 163 | * @constant |
| 164 | * @default |
| 165 | */ |
| 166 | TIMEOUT: 'ERR_MOCHA_TIMEOUT' |
Peter Marshall | 0b95ea1 | 2020-07-02 18:50:04 +0200 | [diff] [blame] | 167 | }; |
| 168 | |
Tim van der Lippe | 6d109a9 | 2021-02-16 16:00:32 +0000 | [diff] [blame^] | 169 | /** |
| 170 | * A set containing all string values of all Mocha error constants, for use by {@link isMochaError}. |
| 171 | * @private |
| 172 | */ |
Peter Marshall | 4e161df | 2020-11-10 13:29:38 +0100 | [diff] [blame] | 173 | const MOCHA_ERRORS = new Set(Object.values(constants)); |
| 174 | |
Peter Marshall | 0b95ea1 | 2020-07-02 18:50:04 +0200 | [diff] [blame] | 175 | /** |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 176 | * Creates an error object to be thrown when no files to be tested could be found using specified pattern. |
| 177 | * |
| 178 | * @public |
Tim van der Lippe | 6d109a9 | 2021-02-16 16:00:32 +0000 | [diff] [blame^] | 179 | * @static |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 180 | * @param {string} message - Error message to be displayed. |
| 181 | * @param {string} pattern - User-specified argument value. |
| 182 | * @returns {Error} instance detailing the error condition |
| 183 | */ |
| 184 | function createNoFilesMatchPatternError(message, pattern) { |
| 185 | var err = new Error(message); |
Peter Marshall | 0b95ea1 | 2020-07-02 18:50:04 +0200 | [diff] [blame] | 186 | err.code = constants.NO_FILES_MATCH_PATTERN; |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 187 | err.pattern = pattern; |
| 188 | return err; |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Creates an error object to be thrown when the reporter specified in the options was not found. |
| 193 | * |
| 194 | * @public |
| 195 | * @param {string} message - Error message to be displayed. |
| 196 | * @param {string} reporter - User-specified reporter value. |
| 197 | * @returns {Error} instance detailing the error condition |
| 198 | */ |
| 199 | function createInvalidReporterError(message, reporter) { |
| 200 | var err = new TypeError(message); |
Peter Marshall | 0b95ea1 | 2020-07-02 18:50:04 +0200 | [diff] [blame] | 201 | err.code = constants.INVALID_REPORTER; |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 202 | err.reporter = reporter; |
| 203 | return err; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Creates an error object to be thrown when the interface specified in the options was not found. |
| 208 | * |
| 209 | * @public |
Tim van der Lippe | 6d109a9 | 2021-02-16 16:00:32 +0000 | [diff] [blame^] | 210 | * @static |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 211 | * @param {string} message - Error message to be displayed. |
| 212 | * @param {string} ui - User-specified interface value. |
| 213 | * @returns {Error} instance detailing the error condition |
| 214 | */ |
| 215 | function createInvalidInterfaceError(message, ui) { |
| 216 | var err = new Error(message); |
Peter Marshall | 0b95ea1 | 2020-07-02 18:50:04 +0200 | [diff] [blame] | 217 | err.code = constants.INVALID_INTERFACE; |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 218 | err.interface = ui; |
| 219 | return err; |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Creates an error object to be thrown when a behavior, option, or parameter is unsupported. |
| 224 | * |
| 225 | * @public |
Tim van der Lippe | 6d109a9 | 2021-02-16 16:00:32 +0000 | [diff] [blame^] | 226 | * @static |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 227 | * @param {string} message - Error message to be displayed. |
| 228 | * @returns {Error} instance detailing the error condition |
| 229 | */ |
| 230 | function createUnsupportedError(message) { |
| 231 | var err = new Error(message); |
Peter Marshall | 0b95ea1 | 2020-07-02 18:50:04 +0200 | [diff] [blame] | 232 | err.code = constants.UNSUPPORTED; |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 233 | return err; |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Creates an error object to be thrown when an argument is missing. |
| 238 | * |
| 239 | * @public |
Tim van der Lippe | 6d109a9 | 2021-02-16 16:00:32 +0000 | [diff] [blame^] | 240 | * @static |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 241 | * @param {string} message - Error message to be displayed. |
| 242 | * @param {string} argument - Argument name. |
| 243 | * @param {string} expected - Expected argument datatype. |
| 244 | * @returns {Error} instance detailing the error condition |
| 245 | */ |
| 246 | function createMissingArgumentError(message, argument, expected) { |
| 247 | return createInvalidArgumentTypeError(message, argument, expected); |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Creates an error object to be thrown when an argument did not use the supported type |
| 252 | * |
| 253 | * @public |
Tim van der Lippe | 6d109a9 | 2021-02-16 16:00:32 +0000 | [diff] [blame^] | 254 | * @static |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 255 | * @param {string} message - Error message to be displayed. |
| 256 | * @param {string} argument - Argument name. |
| 257 | * @param {string} expected - Expected argument datatype. |
| 258 | * @returns {Error} instance detailing the error condition |
| 259 | */ |
| 260 | function createInvalidArgumentTypeError(message, argument, expected) { |
| 261 | var err = new TypeError(message); |
Peter Marshall | 0b95ea1 | 2020-07-02 18:50:04 +0200 | [diff] [blame] | 262 | err.code = constants.INVALID_ARG_TYPE; |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 263 | err.argument = argument; |
| 264 | err.expected = expected; |
| 265 | err.actual = typeof argument; |
| 266 | return err; |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Creates an error object to be thrown when an argument did not use the supported value |
| 271 | * |
| 272 | * @public |
Tim van der Lippe | 6d109a9 | 2021-02-16 16:00:32 +0000 | [diff] [blame^] | 273 | * @static |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 274 | * @param {string} message - Error message to be displayed. |
| 275 | * @param {string} argument - Argument name. |
| 276 | * @param {string} value - Argument value. |
| 277 | * @param {string} [reason] - Why value is invalid. |
| 278 | * @returns {Error} instance detailing the error condition |
| 279 | */ |
| 280 | function createInvalidArgumentValueError(message, argument, value, reason) { |
| 281 | var err = new TypeError(message); |
Peter Marshall | 0b95ea1 | 2020-07-02 18:50:04 +0200 | [diff] [blame] | 282 | err.code = constants.INVALID_ARG_VALUE; |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 283 | err.argument = argument; |
| 284 | err.value = value; |
| 285 | err.reason = typeof reason !== 'undefined' ? reason : 'is invalid'; |
| 286 | return err; |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * Creates an error object to be thrown when an exception was caught, but the `Error` is falsy or undefined. |
| 291 | * |
| 292 | * @public |
Tim van der Lippe | 6d109a9 | 2021-02-16 16:00:32 +0000 | [diff] [blame^] | 293 | * @static |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 294 | * @param {string} message - Error message to be displayed. |
| 295 | * @returns {Error} instance detailing the error condition |
| 296 | */ |
| 297 | function createInvalidExceptionError(message, value) { |
| 298 | var err = new Error(message); |
Peter Marshall | 0b95ea1 | 2020-07-02 18:50:04 +0200 | [diff] [blame] | 299 | err.code = constants.INVALID_EXCEPTION; |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 300 | err.valueType = typeof value; |
| 301 | err.value = value; |
| 302 | return err; |
| 303 | } |
| 304 | |
Peter Marshall | 0b95ea1 | 2020-07-02 18:50:04 +0200 | [diff] [blame] | 305 | /** |
| 306 | * Creates an error object to be thrown when an unrecoverable error occurs. |
| 307 | * |
| 308 | * @public |
Tim van der Lippe | 6d109a9 | 2021-02-16 16:00:32 +0000 | [diff] [blame^] | 309 | * @static |
Peter Marshall | 0b95ea1 | 2020-07-02 18:50:04 +0200 | [diff] [blame] | 310 | * @param {string} message - Error message to be displayed. |
| 311 | * @returns {Error} instance detailing the error condition |
| 312 | */ |
| 313 | function createFatalError(message, value) { |
| 314 | var err = new Error(message); |
| 315 | err.code = constants.FATAL; |
| 316 | err.valueType = typeof value; |
| 317 | err.value = value; |
| 318 | return err; |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Dynamically creates a plugin-type-specific error based on plugin type |
| 323 | * @param {string} message - Error message |
| 324 | * @param {"reporter"|"interface"} pluginType - Plugin type. Future: expand as needed |
| 325 | * @param {string} [pluginId] - Name/path of plugin, if any |
| 326 | * @throws When `pluginType` is not known |
| 327 | * @public |
Tim van der Lippe | 6d109a9 | 2021-02-16 16:00:32 +0000 | [diff] [blame^] | 328 | * @static |
Peter Marshall | 0b95ea1 | 2020-07-02 18:50:04 +0200 | [diff] [blame] | 329 | * @returns {Error} |
| 330 | */ |
Peter Marshall | 4e161df | 2020-11-10 13:29:38 +0100 | [diff] [blame] | 331 | function createInvalidLegacyPluginError(message, pluginType, pluginId) { |
Peter Marshall | 0b95ea1 | 2020-07-02 18:50:04 +0200 | [diff] [blame] | 332 | switch (pluginType) { |
| 333 | case 'reporter': |
| 334 | return createInvalidReporterError(message, pluginId); |
| 335 | case 'interface': |
| 336 | return createInvalidInterfaceError(message, pluginId); |
| 337 | default: |
| 338 | throw new Error('unknown pluginType "' + pluginType + '"'); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | /** |
Peter Marshall | 4e161df | 2020-11-10 13:29:38 +0100 | [diff] [blame] | 343 | * **DEPRECATED**. Use {@link createInvalidLegacyPluginError} instead Dynamically creates a plugin-type-specific error based on plugin type |
| 344 | * @deprecated |
| 345 | * @param {string} message - Error message |
| 346 | * @param {"reporter"|"interface"} pluginType - Plugin type. Future: expand as needed |
| 347 | * @param {string} [pluginId] - Name/path of plugin, if any |
| 348 | * @throws When `pluginType` is not known |
| 349 | * @public |
Tim van der Lippe | 6d109a9 | 2021-02-16 16:00:32 +0000 | [diff] [blame^] | 350 | * @static |
Peter Marshall | 4e161df | 2020-11-10 13:29:38 +0100 | [diff] [blame] | 351 | * @returns {Error} |
| 352 | */ |
| 353 | function createInvalidPluginError(...args) { |
| 354 | deprecate('Use createInvalidLegacyPluginError() instead'); |
| 355 | return createInvalidLegacyPluginError(...args); |
| 356 | } |
| 357 | |
| 358 | /** |
Peter Marshall | 0b95ea1 | 2020-07-02 18:50:04 +0200 | [diff] [blame] | 359 | * Creates an error object to be thrown when a mocha object's `run` method is executed while it is already disposed. |
| 360 | * @param {string} message The error message to be displayed. |
| 361 | * @param {boolean} cleanReferencesAfterRun the value of `cleanReferencesAfterRun` |
| 362 | * @param {Mocha} instance the mocha instance that throw this error |
Tim van der Lippe | 6d109a9 | 2021-02-16 16:00:32 +0000 | [diff] [blame^] | 363 | * @static |
Peter Marshall | 0b95ea1 | 2020-07-02 18:50:04 +0200 | [diff] [blame] | 364 | */ |
| 365 | function createMochaInstanceAlreadyDisposedError( |
| 366 | message, |
| 367 | cleanReferencesAfterRun, |
| 368 | instance |
| 369 | ) { |
| 370 | var err = new Error(message); |
| 371 | err.code = constants.INSTANCE_ALREADY_DISPOSED; |
| 372 | err.cleanReferencesAfterRun = cleanReferencesAfterRun; |
| 373 | err.instance = instance; |
| 374 | return err; |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * Creates an error object to be thrown when a mocha object's `run` method is called while a test run is in progress. |
| 379 | * @param {string} message The error message to be displayed. |
Tim van der Lippe | 6d109a9 | 2021-02-16 16:00:32 +0000 | [diff] [blame^] | 380 | * @static |
| 381 | * @public |
Peter Marshall | 0b95ea1 | 2020-07-02 18:50:04 +0200 | [diff] [blame] | 382 | */ |
| 383 | function createMochaInstanceAlreadyRunningError(message, instance) { |
| 384 | var err = new Error(message); |
| 385 | err.code = constants.INSTANCE_ALREADY_RUNNING; |
| 386 | err.instance = instance; |
| 387 | return err; |
| 388 | } |
| 389 | |
Tim van der Lippe | 6d109a9 | 2021-02-16 16:00:32 +0000 | [diff] [blame^] | 390 | /** |
Peter Marshall | 0b95ea1 | 2020-07-02 18:50:04 +0200 | [diff] [blame] | 391 | * Creates an error object to be thrown when done() is called multiple times in a test |
| 392 | * |
| 393 | * @public |
| 394 | * @param {Runnable} runnable - Original runnable |
| 395 | * @param {Error} [originalErr] - Original error, if any |
| 396 | * @returns {Error} instance detailing the error condition |
Tim van der Lippe | 6d109a9 | 2021-02-16 16:00:32 +0000 | [diff] [blame^] | 397 | * @static |
Peter Marshall | 0b95ea1 | 2020-07-02 18:50:04 +0200 | [diff] [blame] | 398 | */ |
| 399 | function createMultipleDoneError(runnable, originalErr) { |
| 400 | var title; |
| 401 | try { |
| 402 | title = format('<%s>', runnable.fullTitle()); |
| 403 | if (runnable.parent.root) { |
| 404 | title += ' (of root suite)'; |
| 405 | } |
| 406 | } catch (ignored) { |
| 407 | title = format('<%s> (of unknown suite)', runnable.title); |
| 408 | } |
| 409 | var message = format( |
| 410 | 'done() called multiple times in %s %s', |
| 411 | runnable.type ? runnable.type : 'unknown runnable', |
| 412 | title |
| 413 | ); |
| 414 | if (runnable.file) { |
| 415 | message += format(' of file %s', runnable.file); |
| 416 | } |
| 417 | if (originalErr) { |
| 418 | message += format('; in addition, done() received error: %s', originalErr); |
| 419 | } |
| 420 | |
| 421 | var err = new Error(message); |
| 422 | err.code = constants.MULTIPLE_DONE; |
| 423 | err.valueType = typeof originalErr; |
| 424 | err.value = originalErr; |
| 425 | return err; |
| 426 | } |
| 427 | |
| 428 | /** |
| 429 | * Creates an error object to be thrown when `.only()` is used with |
| 430 | * `--forbid-only`. |
Tim van der Lippe | 6d109a9 | 2021-02-16 16:00:32 +0000 | [diff] [blame^] | 431 | * @static |
Peter Marshall | 0b95ea1 | 2020-07-02 18:50:04 +0200 | [diff] [blame] | 432 | * @public |
| 433 | * @param {Mocha} mocha - Mocha instance |
| 434 | * @returns {Error} Error with code {@link constants.FORBIDDEN_EXCLUSIVITY} |
| 435 | */ |
| 436 | function createForbiddenExclusivityError(mocha) { |
| 437 | var err = new Error( |
| 438 | mocha.isWorker |
| 439 | ? '`.only` is not supported in parallel mode' |
| 440 | : '`.only` forbidden by --forbid-only' |
| 441 | ); |
| 442 | err.code = constants.FORBIDDEN_EXCLUSIVITY; |
| 443 | return err; |
| 444 | } |
| 445 | |
Peter Marshall | 4e161df | 2020-11-10 13:29:38 +0100 | [diff] [blame] | 446 | /** |
| 447 | * Creates an error object to be thrown when a plugin definition is invalid |
Tim van der Lippe | 6d109a9 | 2021-02-16 16:00:32 +0000 | [diff] [blame^] | 448 | * @static |
Peter Marshall | 4e161df | 2020-11-10 13:29:38 +0100 | [diff] [blame] | 449 | * @param {string} msg - Error message |
| 450 | * @param {PluginDefinition} [pluginDef] - Problematic plugin definition |
| 451 | * @public |
| 452 | * @returns {Error} Error with code {@link constants.INVALID_PLUGIN_DEFINITION} |
| 453 | */ |
| 454 | function createInvalidPluginDefinitionError(msg, pluginDef) { |
| 455 | const err = new Error(msg); |
| 456 | err.code = constants.INVALID_PLUGIN_DEFINITION; |
| 457 | err.pluginDef = pluginDef; |
| 458 | return err; |
| 459 | } |
| 460 | |
| 461 | /** |
| 462 | * Creates an error object to be thrown when a plugin implementation (user code) is invalid |
Tim van der Lippe | 6d109a9 | 2021-02-16 16:00:32 +0000 | [diff] [blame^] | 463 | * @static |
Peter Marshall | 4e161df | 2020-11-10 13:29:38 +0100 | [diff] [blame] | 464 | * @param {string} msg - Error message |
| 465 | * @param {Object} [opts] - Plugin definition and user-supplied implementation |
| 466 | * @param {PluginDefinition} [opts.pluginDef] - Plugin Definition |
| 467 | * @param {*} [opts.pluginImpl] - Plugin Implementation (user-supplied) |
| 468 | * @public |
| 469 | * @returns {Error} Error with code {@link constants.INVALID_PLUGIN_DEFINITION} |
| 470 | */ |
| 471 | function createInvalidPluginImplementationError( |
| 472 | msg, |
| 473 | {pluginDef, pluginImpl} = {} |
| 474 | ) { |
| 475 | const err = new Error(msg); |
| 476 | err.code = constants.INVALID_PLUGIN_IMPLEMENTATION; |
| 477 | err.pluginDef = pluginDef; |
| 478 | err.pluginImpl = pluginImpl; |
| 479 | return err; |
| 480 | } |
| 481 | |
| 482 | /** |
Tim van der Lippe | 6d109a9 | 2021-02-16 16:00:32 +0000 | [diff] [blame^] | 483 | * Creates an error object to be thrown when a runnable exceeds its allowed run time. |
| 484 | * @static |
| 485 | * @param {string} msg - Error message |
| 486 | * @param {number} [timeout] - Timeout in ms |
| 487 | * @param {string} [file] - File, if given |
| 488 | * @returns {MochaTimeoutError} |
| 489 | */ |
| 490 | function createTimeoutError(msg, timeout, file) { |
| 491 | const err = new Error(msg); |
| 492 | err.code = constants.TIMEOUT; |
| 493 | err.timeout = timeout; |
| 494 | err.file = file; |
| 495 | return err; |
| 496 | } |
| 497 | |
| 498 | /** |
Peter Marshall | 4e161df | 2020-11-10 13:29:38 +0100 | [diff] [blame] | 499 | * Returns `true` if an error came out of Mocha. |
| 500 | * _Can suffer from false negatives, but not false positives._ |
Tim van der Lippe | 6d109a9 | 2021-02-16 16:00:32 +0000 | [diff] [blame^] | 501 | * @static |
Peter Marshall | 4e161df | 2020-11-10 13:29:38 +0100 | [diff] [blame] | 502 | * @public |
| 503 | * @param {*} err - Error, or anything |
| 504 | * @returns {boolean} |
| 505 | */ |
| 506 | const isMochaError = err => |
| 507 | Boolean(err && typeof err === 'object' && MOCHA_ERRORS.has(err.code)); |
| 508 | |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 509 | module.exports = { |
Peter Marshall | 4e161df | 2020-11-10 13:29:38 +0100 | [diff] [blame] | 510 | constants, |
| 511 | createFatalError, |
| 512 | createForbiddenExclusivityError, |
| 513 | createInvalidArgumentTypeError, |
| 514 | createInvalidArgumentValueError, |
| 515 | createInvalidExceptionError, |
| 516 | createInvalidInterfaceError, |
| 517 | createInvalidLegacyPluginError, |
| 518 | createInvalidPluginDefinitionError, |
| 519 | createInvalidPluginError, |
| 520 | createInvalidPluginImplementationError, |
| 521 | createInvalidReporterError, |
| 522 | createMissingArgumentError, |
| 523 | createMochaInstanceAlreadyDisposedError, |
| 524 | createMochaInstanceAlreadyRunningError, |
| 525 | createMultipleDoneError, |
| 526 | createNoFilesMatchPatternError, |
Tim van der Lippe | 6d109a9 | 2021-02-16 16:00:32 +0000 | [diff] [blame^] | 527 | createTimeoutError, |
Peter Marshall | 4e161df | 2020-11-10 13:29:38 +0100 | [diff] [blame] | 528 | createUnsupportedError, |
| 529 | deprecate, |
| 530 | isMochaError, |
| 531 | warn |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 532 | }; |
Tim van der Lippe | 6d109a9 | 2021-02-16 16:00:32 +0000 | [diff] [blame^] | 533 | |
| 534 | /** |
| 535 | * The error thrown when a Runnable times out |
| 536 | * @memberof module:lib/errors |
| 537 | * @typedef {Error} MochaTimeoutError |
| 538 | * @property {constants.TIMEOUT} code - Error code |
| 539 | * @property {number?} timeout Timeout in ms |
| 540 | * @property {string?} file Filepath, if given |
| 541 | */ |