Skip to content

Commit 4d940b6

Browse files
authored
Show error on failing to parse .tldrrc (#366)
1 parent be87030 commit 4d940b6

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

lib/config.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ exports.get = () => {
1616
try {
1717
customConfig = JSON.parse(fs.readFileSync(CUSTOM));
1818
} catch (ex) {
19-
/*eslint-disable */
20-
/*eslint-enable */
19+
if (ex instanceof SyntaxError) {
20+
throw new Error('The content of .tldrrc is not a valid JSON object:\n' + ex);
21+
}
2122
}
2223

2324
let merged = defaults(customConfig, defaultConfig);

test/config.spec.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ describe('Config', () => {
1818
"repository": "http://myrepo/assets/tldr.zip"
1919
}`;
2020

21-
const CUSTOM_INVALID =
21+
const CUSTOM_INVALID_JSON =
22+
`# comments are not allowed in json
23+
{}`;
24+
25+
const CUSTOM_INVALID_SCHEMA =
2226
`
2327
{
2428
"themes": {
@@ -53,9 +57,15 @@ describe('Config', () => {
5357
config.get().repository.should.eql('http://myrepo/assets/tldr.zip');
5458
});
5559

56-
it('should validate the custom config format', () => {
60+
it('should validate the custom config JSON', () => {
61+
fs.readFileSync.onCall(0).returns(DEFAULT);
62+
fs.readFileSync.onCall(1).returns(CUSTOM_INVALID_JSON);
63+
config.get.should.throw(/not a valid JSON object/);
64+
});
65+
66+
it('should validate the custom config schema', () => {
5767
fs.readFileSync.onCall(0).returns(DEFAULT);
58-
fs.readFileSync.onCall(1).returns(CUSTOM_INVALID);
68+
fs.readFileSync.onCall(1).returns(CUSTOM_INVALID_SCHEMA);
5969
config.get.should.throw(/Invalid theme value/);
6070
});
6171

0 commit comments

Comments
 (0)