-
-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added custom error objects including code #3467
Changes from 15 commits
02514bc
346aeee
57d2e99
1d3b15e
59b7368
aa02b83
462a9ae
356c8b1
716e75f
ddbb30e
5e69794
4ff2807
b754a53
b478d6f
2088133
3c2f725
713533d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,8 @@ | |
|
||
const Mocha = require('../mocha'); | ||
const ansi = require('ansi-colors'); | ||
var errors = require('../errors'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. const? |
||
var createInvalidArgumentValueError = errors.createInvalidArgumentValueError; | ||
|
||
const { | ||
list, | ||
|
@@ -190,7 +192,12 @@ exports.builder = yargs => | |
const pair = opt.split('='); | ||
|
||
if (pair.length > 2 || !pair.length) { | ||
throw new Error(`invalid reporter option '${opt}'`); | ||
throw createInvalidArgumentValueError( | ||
`invalid reporter option '${opt}'`, | ||
'--reporter-option', | ||
opt, | ||
'expected "key=value" format' | ||
); | ||
} | ||
|
||
acc[pair[0]] = pair.length === 2 ? pair[1] : true; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
'use strict'; | ||
/** | ||
* @module Errors | ||
*/ | ||
/** | ||
* Factory functions to create throwable error objects | ||
*/ | ||
|
||
plroebuck marked this conversation as resolved.
Show resolved
Hide resolved
plroebuck marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/** | ||
* Creates an error object used when no files to be tested could be found using specified pattern. | ||
* | ||
* @public | ||
plroebuck marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* @param {string} message - Error message to be displayed. | ||
* @param {string} pattern - User-specified argument value. | ||
* @returns {Error} instance detailing the error condition | ||
*/ | ||
function createNoFilesMatchPatternError(message, pattern) { | ||
var err = new Error(message); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In a later revision (PR), we should generate the messages using additional arguments rather than requiring the caller to do so. var err = new Error(`{message}: pattern: "{pattern}"`); But let's get what you've done thusfar in place first... |
||
err.code = 'ERR_MOCHA_NO_FILES_MATCH_PATTERN'; | ||
err.pattern = pattern; | ||
return err; | ||
} | ||
|
||
/** | ||
* Creates an error object used when the reporter specified in the options was not found. | ||
* | ||
* @public | ||
* @param {string} message - Error message to be displayed. | ||
* @param {string} reporter - User-specified reporter value. | ||
* @returns {Error} instance detailing the error condition | ||
*/ | ||
function createInvalidReporterError(message, reporter) { | ||
var err = new TypeError(message); | ||
err.code = 'ERR_MOCHA_INVALID_REPORTER'; | ||
err.reporter = reporter; | ||
return err; | ||
} | ||
|
||
/** | ||
* Creates an error object used when the interface specified in the options was not found. | ||
* | ||
* @public | ||
* @param {string} message - Error message to be displayed. | ||
* @param {string} ui - User-specified interface value. | ||
* @returns {Error} instance detailing the error condition | ||
*/ | ||
function createInvalidInterfaceError(message, ui) { | ||
var err = new Error(message); | ||
err.code = 'ERR_MOCHA_INVALID_INTERFACE'; | ||
err.interface = ui; | ||
return err; | ||
} | ||
|
||
/** | ||
* Creates an error object used when the type of output specified was not supported. | ||
* | ||
* @public | ||
* @param {string} message - Error message to be displayed. | ||
* @returns {Error} instance detailing the error condition | ||
*/ | ||
function createNotSupportedError(message) { | ||
var err = new Error(message); | ||
err.code = 'ERR_MOCHA_NOT_SUPPORTED'; | ||
return err; | ||
} | ||
|
||
/** | ||
* Creates an error object used when an argument is missing. | ||
* | ||
* @public | ||
* @param {string} message - Error message to be displayed. | ||
* @param {string} argument - Argument name. | ||
* @param {string} expected - Expected argument datatype. | ||
* @returns {Error} instance detailing the error condition | ||
*/ | ||
function createMissingArgumentError(message, argument, expected) { | ||
return createInvalidArgumentTypeError(message, argument, expected); | ||
} | ||
plroebuck marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* Creates an error object used when an argument did not use the supported type | ||
* | ||
* @public | ||
* @param {string} message - Error message to be displayed. | ||
* @param {string} argument - Argument name. | ||
* @param {string} expected - Expected argument datatype. | ||
* @returns {Error} instance detailing the error condition | ||
*/ | ||
function createInvalidArgumentTypeError(message, argument, expected) { | ||
var err = new TypeError(message); | ||
plroebuck marked this conversation as resolved.
Show resolved
Hide resolved
|
||
err.code = 'ERR_MOCHA_INVALID_ARG_TYPE'; | ||
err.argument = argument; | ||
err.expected = expected; | ||
err.actual = typeof argument; | ||
return err; | ||
} | ||
|
||
/** | ||
* Creates an error object used when an argument did not use the supported value | ||
* | ||
* @public | ||
* @param {string} message - Error message to be displayed. | ||
* @param {string} argument - Argument name. | ||
* @param {string} value - Argument value. | ||
* @param {string} reason - Why value is invalid. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. * @param {string} [reason] - Why value is invalid. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why square brackets? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
* @returns {Error} instance detailing the error condition | ||
*/ | ||
function createInvalidArgumentValueError(message, argument, value, reason) { | ||
var err = new TypeError(message); | ||
err.code = 'ERR_MOCHA_INVALID_ARG_VALUE'; | ||
err.argument = argument; | ||
err.value = value; | ||
err.reason = typeof reason !== 'undefined' ? reason : 'is invalid'; | ||
return err; | ||
} | ||
|
||
plroebuck marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/** | ||
* Creates an error object used when an error was thrown but no details were specified. | ||
* | ||
* @public | ||
* @param {string} message - Error message to be displayed. | ||
* @returns {Error} instance detailing the error condition | ||
*/ | ||
function createUndefinedError(message) { | ||
var err = new Error(message); | ||
err.code = 'ERR_MOCHA_UNDEFINED_ERROR'; | ||
return err; | ||
} | ||
|
||
module.exports = { | ||
createInvalidArgumentTypeError: createInvalidArgumentTypeError, | ||
plroebuck marked this conversation as resolved.
Show resolved
Hide resolved
|
||
createInvalidArgumentValueError: createInvalidArgumentValueError, | ||
createInvalidInterfaceError: createInvalidInterfaceError, | ||
createInvalidReporterError: createInvalidReporterError, | ||
createMissingArgumentError: createMissingArgumentError, | ||
createNoFilesMatchPatternError: createNoFilesMatchPatternError, | ||
createNotSupportedError: createNotSupportedError, | ||
createUndefinedError: createUndefinedError | ||
}; | ||
plroebuck marked this conversation as resolved.
Show resolved
Hide resolved
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
future PR: use variables instead of hardcoded strings
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regrettably, it's the standard Node
Error.code
processing pattern.This seems more like what I would have preferred -- exported constants.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, I had it like that on first iteration of PR but the change was lost in the ether. Will look at updating.