Skip to content

Commit 0079ae7

Browse files
authored
Change CLI file parsing errors to use an error code (#4502)
1 parent d35eb9d commit 0079ae7

File tree

6 files changed

+54
-7
lines changed

6 files changed

+54
-7
lines changed

lib/cli/config.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const fs = require('fs');
1111
const path = require('path');
1212
const debug = require('debug')('mocha:cli:config');
1313
const findUp = require('find-up');
14+
const {createUnparsableFileError} = require('../errors');
1415
const utils = require('../utils');
1516

1617
/**
@@ -81,7 +82,10 @@ exports.loadConfig = filepath => {
8182
config = parsers.json(filepath);
8283
}
8384
} catch (err) {
84-
throw new Error(`failed to parse config "${filepath}": ${err}`);
85+
throw createUnparsableFileError(
86+
`Unable to read/parse ${filepath}: ${err}`,
87+
filepath
88+
);
8589
}
8690
return config;
8791
};

lib/cli/options.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const {loadConfig, findConfig} = require('./config');
1818
const findUp = require('find-up');
1919
const debug = require('debug')('mocha:cli:options');
2020
const {isNodeFlag} = require('./node-flags');
21+
const {createUnparsableFileError} = require('../errors');
2122

2223
/**
2324
* The `yargs-parser` namespace
@@ -190,7 +191,10 @@ const loadPkgRc = (args = {}) => {
190191
}
191192
} catch (err) {
192193
if (args.package) {
193-
throw new Error(`Unable to read/parse ${filepath}: ${err}`);
194+
throw createUnparsableFileError(
195+
`Unable to read/parse ${filepath}: ${err}`,
196+
filepath
197+
);
194198
}
195199
debug('failed to read default package.json at %s; ignoring', filepath);
196200
}

lib/errors.js

+23-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,14 @@ var constants = {
163163
* @constant
164164
* @default
165165
*/
166-
TIMEOUT: 'ERR_MOCHA_TIMEOUT'
166+
TIMEOUT: 'ERR_MOCHA_TIMEOUT',
167+
168+
/**
169+
* Input file is not able to be parsed
170+
* @constant
171+
* @default
172+
*/
173+
UNPARSABLE_FILE: 'ERR_MOCHA_UNPARSABLE_FILE'
167174
};
168175

169176
/**
@@ -495,6 +502,20 @@ function createTimeoutError(msg, timeout, file) {
495502
return err;
496503
}
497504

505+
/**
506+
* Creates an error object to be thrown when file is unparsable
507+
* @public
508+
* @static
509+
* @param {string} message - Error message to be displayed.
510+
* @param {string} filename - File name
511+
* @returns {Error} Error with code {@link constants.UNPARSABLE_FILE}
512+
*/
513+
function createUnparsableFileError(message, filename) {
514+
var err = new Error(message);
515+
err.code = constants.UNPARSABLE_FILE;
516+
return err;
517+
}
518+
498519
/**
499520
* Returns `true` if an error came out of Mocha.
500521
* _Can suffer from false negatives, but not false positives._
@@ -525,6 +546,7 @@ module.exports = {
525546
createMultipleDoneError,
526547
createNoFilesMatchPatternError,
527548
createTimeoutError,
549+
createUnparsableFileError,
528550
createUnsupportedError,
529551
deprecate,
530552
isMochaError,

test/node-unit/cli/config.spec.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,15 @@ describe('cli/config', function() {
109109

110110
describe('when config file parsing fails', function() {
111111
beforeEach(function() {
112-
sinon.stub(parsers, 'yaml').throws();
112+
sinon.stub(parsers, 'yaml').throws('goo.yaml is unparsable');
113113
});
114114

115115
it('should throw', function() {
116-
expect(() => loadConfig('goo.yaml'), 'to throw', /failed to parse/);
116+
expect(
117+
() => loadConfig('goo.yaml'),
118+
'to throw',
119+
'Unable to read/parse goo.yaml: goo.yaml is unparsable'
120+
);
117121
});
118122
});
119123
});

test/node-unit/cli/options.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ describe('options', function() {
130130
beforeEach(function() {
131131
readFileSync = sinon.stub();
132132
// package.json
133-
readFileSync.onFirstCall().throws('yikes');
133+
readFileSync.onFirstCall().throws('bad file message');
134134
findConfig = sinon.stub().returns('/some/.mocharc.json');
135135
loadConfig = sinon.stub().returns({});
136136
findupSync = sinon.stub();
@@ -148,7 +148,7 @@ describe('options', function() {
148148
loadOptions('--package /something/wherever --require butts');
149149
},
150150
'to throw',
151-
/unable to read\/parse/i
151+
'Unable to read/parse /something/wherever: bad file message'
152152
);
153153
});
154154
});

test/unit/errors.spec.js

+13
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,19 @@ describe('Errors', function() {
6969
});
7070
});
7171

72+
describe('createUnparsableFileError()', function() {
73+
it('should include expected code in thrown unparsable file errors', function() {
74+
expect(
75+
errors.createUnparsableFileError(message, 'badFilePath'),
76+
'to satisfy',
77+
{
78+
message: message,
79+
code: 'ERR_MOCHA_UNPARSABLE_FILE'
80+
}
81+
);
82+
});
83+
});
84+
7285
describe('deprecate()', function() {
7386
var emitWarning;
7487

0 commit comments

Comments
 (0)