blob: fafee70eee8afc7df4dac7f7dad8f9e27d93ae7b [file] [log] [blame]
Yang Guo4fd355c2019-09-19 10:59:03 +02001'use strict';
2/**
3 * @module Errors
4 */
5/**
6 * Factory functions to create throwable error objects
7 */
8
9/**
10 * Creates an error object to be thrown when no files to be tested could be found using specified pattern.
11 *
12 * @public
13 * @param {string} message - Error message to be displayed.
14 * @param {string} pattern - User-specified argument value.
15 * @returns {Error} instance detailing the error condition
16 */
17function createNoFilesMatchPatternError(message, pattern) {
18 var err = new Error(message);
19 err.code = 'ERR_MOCHA_NO_FILES_MATCH_PATTERN';
20 err.pattern = pattern;
21 return err;
22}
23
24/**
25 * Creates an error object to be thrown when the reporter specified in the options was not found.
26 *
27 * @public
28 * @param {string} message - Error message to be displayed.
29 * @param {string} reporter - User-specified reporter value.
30 * @returns {Error} instance detailing the error condition
31 */
32function createInvalidReporterError(message, reporter) {
33 var err = new TypeError(message);
34 err.code = 'ERR_MOCHA_INVALID_REPORTER';
35 err.reporter = reporter;
36 return err;
37}
38
39/**
40 * Creates an error object to be thrown when the interface specified in the options was not found.
41 *
42 * @public
43 * @param {string} message - Error message to be displayed.
44 * @param {string} ui - User-specified interface value.
45 * @returns {Error} instance detailing the error condition
46 */
47function createInvalidInterfaceError(message, ui) {
48 var err = new Error(message);
49 err.code = 'ERR_MOCHA_INVALID_INTERFACE';
50 err.interface = ui;
51 return err;
52}
53
54/**
55 * Creates an error object to be thrown when a behavior, option, or parameter is unsupported.
56 *
57 * @public
58 * @param {string} message - Error message to be displayed.
59 * @returns {Error} instance detailing the error condition
60 */
61function createUnsupportedError(message) {
62 var err = new Error(message);
63 err.code = 'ERR_MOCHA_UNSUPPORTED';
64 return err;
65}
66
67/**
68 * Creates an error object to be thrown when an argument is missing.
69 *
70 * @public
71 * @param {string} message - Error message to be displayed.
72 * @param {string} argument - Argument name.
73 * @param {string} expected - Expected argument datatype.
74 * @returns {Error} instance detailing the error condition
75 */
76function createMissingArgumentError(message, argument, expected) {
77 return createInvalidArgumentTypeError(message, argument, expected);
78}
79
80/**
81 * Creates an error object to be thrown when an argument did not use the supported type
82 *
83 * @public
84 * @param {string} message - Error message to be displayed.
85 * @param {string} argument - Argument name.
86 * @param {string} expected - Expected argument datatype.
87 * @returns {Error} instance detailing the error condition
88 */
89function createInvalidArgumentTypeError(message, argument, expected) {
90 var err = new TypeError(message);
91 err.code = 'ERR_MOCHA_INVALID_ARG_TYPE';
92 err.argument = argument;
93 err.expected = expected;
94 err.actual = typeof argument;
95 return err;
96}
97
98/**
99 * Creates an error object to be thrown when an argument did not use the supported value
100 *
101 * @public
102 * @param {string} message - Error message to be displayed.
103 * @param {string} argument - Argument name.
104 * @param {string} value - Argument value.
105 * @param {string} [reason] - Why value is invalid.
106 * @returns {Error} instance detailing the error condition
107 */
108function createInvalidArgumentValueError(message, argument, value, reason) {
109 var err = new TypeError(message);
110 err.code = 'ERR_MOCHA_INVALID_ARG_VALUE';
111 err.argument = argument;
112 err.value = value;
113 err.reason = typeof reason !== 'undefined' ? reason : 'is invalid';
114 return err;
115}
116
117/**
118 * Creates an error object to be thrown when an exception was caught, but the `Error` is falsy or undefined.
119 *
120 * @public
121 * @param {string} message - Error message to be displayed.
122 * @returns {Error} instance detailing the error condition
123 */
124function createInvalidExceptionError(message, value) {
125 var err = new Error(message);
126 err.code = 'ERR_MOCHA_INVALID_EXCEPTION';
127 err.valueType = typeof value;
128 err.value = value;
129 return err;
130}
131
132module.exports = {
133 createInvalidArgumentTypeError: createInvalidArgumentTypeError,
134 createInvalidArgumentValueError: createInvalidArgumentValueError,
135 createInvalidExceptionError: createInvalidExceptionError,
136 createInvalidInterfaceError: createInvalidInterfaceError,
137 createInvalidReporterError: createInvalidReporterError,
138 createMissingArgumentError: createMissingArgumentError,
139 createNoFilesMatchPatternError: createNoFilesMatchPatternError,
140 createUnsupportedError: createUnsupportedError
141};