Skip to content

Commit 6362c21

Browse files
committed
Make config errors more readable in logs
1 parent 3d5c845 commit 6362c21

File tree

5 files changed

+10
-229
lines changed

5 files changed

+10
-229
lines changed

packages/core/src/core/config/config-not-found.error.spec.ts

Lines changed: 2 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -4,65 +4,6 @@ import { strictEqual } from 'assert';
44
// FoalTS
55
import { ConfigNotFoundError } from './config-not-found.error';
66

7-
const errMessage = (msg: string) => `
8-
9-
--------------------------------------------------------
10-
| |
11-
| JSON file (config/default.json, config/test.json, ...) |
12-
| |
13-
| -------------------------------------------------------- |
14-
| |
15-
| { |
16-
| "settings": { |
17-
| "session": { |
18-
| "store": <your_value> |
19-
| // OR with an environment variable: |
20-
| "store": "env(<YOUR_ENVIRONMENT_VARIABLE>)" |
21-
| } |
22-
| } |
23-
| } |
24-
| |
25-
--------------------------------------------------------
26-
27-
--------------------------------------------------------
28-
| |
29-
| YAML file (config/default.yml, config/test.yml, ...) |
30-
| |
31-
| -------------------------------------------------------- |
32-
| |
33-
| settings: |
34-
| session: |
35-
| store: <your_value> |
36-
| # OR with an environment variable: |
37-
| store: env(<YOUR_ENVIRONMENT_VARIABLE>) |
38-
| |
39-
--------------------------------------------------------
40-
41-
--------------------------------------------------------
42-
| |
43-
| JS file (config/default.js, config/test.js, ...) |
44-
| |
45-
| -------------------------------------------------------- |
46-
| |
47-
| const { Env } = require('@foal/core'); |
48-
| |
49-
| { |
50-
| settings: { |
51-
| session: { |
52-
| store: <your_value> |
53-
| // OR with an environment variable: |
54-
| store: Env.get('<YOUR_ENVIRONMENT_VARIABLE>') |
55-
| } |
56-
| } |
57-
| } |
58-
| |
59-
--------------------------------------------------------
60-
61-
No value found for the configuration key "settings.session.store".${msg}
62-
63-
To pass a value, use one of the examples above.
64-
`;
65-
667
describe('ConfigNotFoundError', () => {
678

689
it('should set the property "key" and "msg" from the constructor.', () => {
@@ -76,12 +17,12 @@ describe('ConfigNotFoundError', () => {
7617

7718
it('should have the proper message.', () => {
7819
const err = new ConfigNotFoundError('settings.session.store');
79-
strictEqual(err.message, errMessage(''));
20+
strictEqual(err.message, `No value found for the configuration key "settings.session.store".`);
8021
});
8122

8223
it('should have the proper message (custom message).', () => {
8324
const err = new ConfigNotFoundError('settings.session.store', 'Custom message');
84-
strictEqual(err.message, errMessage('\n\nCustom message'));
25+
strictEqual(err.message, `No value found for the configuration key "settings.session.store". Custom message`);
8526
});
8627

8728
});
Lines changed: 2 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,10 @@
1-
import { makeBox } from './utils';
2-
31
export class ConfigNotFoundError extends Error {
42
readonly name = 'ConfigNotFoundError';
53

64
constructor(readonly key: string, readonly msg?: string) {
75
super();
8-
const keywords = key.split('.');
9-
10-
function generateContent(type: 'JSON'|'YAML'|'JS'): string[] {
11-
const lines: string[] = [];
12-
13-
if (type === 'JS') {
14-
lines.push(' const { Env } = require(\'@foal/core\');');
15-
lines.push('');
16-
}
17-
18-
if (type !== 'YAML') {
19-
lines.push(' {');
20-
}
21-
22-
keywords.forEach((keyword, index) => {
23-
if (type === 'JSON') {
24-
keyword = `"${keyword}"`;
25-
}
26-
27-
const spaces = ' '.repeat(index + (type === 'YAML' ? 1 : 2));
28-
29-
if (index === keywords.length - 1) {
30-
lines.push(spaces + keyword + ': <your_value>');
31-
if (type === 'YAML') {
32-
lines.push(spaces + '# OR with an environment variable: ');
33-
} else {
34-
lines.push(spaces + '// OR with an environment variable: ');
35-
}
36-
37-
let envValue = '';
38-
switch (type) {
39-
case 'JS':
40-
envValue = 'Env.get(\'<YOUR_ENVIRONMENT_VARIABLE>\')';
41-
break;
42-
case 'JSON':
43-
envValue = '"env(<YOUR_ENVIRONMENT_VARIABLE>)"';
44-
break;
45-
case 'YAML':
46-
envValue = 'env(<YOUR_ENVIRONMENT_VARIABLE>)';
47-
break;
48-
}
49-
lines.push(spaces + keyword + ': ' + envValue);
50-
} else {
51-
if (type === 'YAML') {
52-
lines.push(spaces + keyword + ':');
53-
} else {
54-
lines.push(spaces + keyword + ': {');
55-
}
56-
}
57-
});
58-
59-
if (type !== 'YAML') {
60-
keywords.forEach((_, index) => {
61-
if (index === keywords.length - 1) {
62-
return;
63-
}
64-
const spaces = ' '.repeat(keywords.length - index);
65-
lines.push(spaces + '}');
66-
});
67-
68-
lines.push(' }');
69-
}
70-
71-
return lines;
72-
}
73-
74-
this.message = '\n\n'
75-
+ makeBox(
76-
'JSON file (config/default.json, config/test.json, ...)',
77-
generateContent('JSON'),
78-
)
79-
+ '\n'
80-
+ makeBox(
81-
'YAML file (config/default.yml, config/test.yml, ...)',
82-
generateContent('YAML'),
83-
)
84-
+ '\n'
85-
+ makeBox(
86-
'JS file (config/default.js, config/test.js, ...)',
87-
generateContent('JS'),
88-
)
89-
+ '\n'
90-
+ `No value found for the configuration key "${key}".\n`
91-
+ (msg === undefined ? '' : `\n${msg}\n`)
92-
+ '\n'
93-
+ `To pass a value, use one of the examples above.\n`;
6+
7+
this.message = `No value found for the configuration key "${key}".${msg === undefined ? '' : ` ${msg}`}`;
948
}
959

9610
}

packages/core/src/core/config/config-type.error.spec.ts

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,6 @@ import { strictEqual } from 'assert';
44
// FoalTS
55
import { ConfigTypeError } from './config-type.error';
66

7-
const errMessage = `
8-
9-
--------------------------------------------------------
10-
| |
11-
| Configuration file |
12-
| |
13-
| -------------------------------------------------------- |
14-
| |
15-
| { |
16-
| settings: { |
17-
| session: { |
18-
|-> store: ************* |
19-
| } |
20-
| } |
21-
| } |
22-
| |
23-
--------------------------------------------------------
24-
25-
The value of the configuration key "settings.session.store" has an invalid type.
26-
27-
Expected a "string", but got a "boolean".
28-
`;
29-
307
describe('ConfigTypeError', () => {
318

329
it('should set three properties "key", "expected" and "actual" from the constructor.', () => {
@@ -38,7 +15,11 @@ describe('ConfigTypeError', () => {
3815

3916
it('should have the proper message.', () => {
4017
const err = new ConfigTypeError('settings.session.store', 'string', 'boolean');
41-
strictEqual(err.message, errMessage);
18+
19+
const expected = `The value of the configuration key "settings.session.store" has an invalid type. Expected a "string", but got a "boolean".`;
20+
const actual = err.message;
21+
22+
strictEqual(actual, expected);
4223
});
4324

4425
});
Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,8 @@
1-
import { makeBox } from './utils';
2-
31
export class ConfigTypeError extends Error {
42
readonly name = 'ConfigTypeError';
53

64
constructor(readonly key: string, readonly expected: string, readonly actual: string) {
75
super();
8-
const keywords = key.split('.');
9-
10-
const lines: string[] = [];
11-
12-
lines.push(' {');
13-
14-
keywords.forEach((keyword, index) => {
15-
const spaces = ' '.repeat(index + 1);
16-
if (index === keywords.length - 1) {
17-
lines.push('->' + spaces + keyword + ': *************');
18-
} else {
19-
lines.push(' ' + spaces + keyword + ': {');
20-
}
21-
});
22-
23-
keywords.forEach((_, index) => {
24-
if (index === keywords.length - 1) {
25-
return;
26-
}
27-
const spaces = ' '.repeat(keywords.length - index);
28-
lines.push(spaces + '}');
29-
});
30-
31-
lines.push(' }');
32-
33-
this.message = '\n\n'
34-
+ makeBox(
35-
'Configuration file',
36-
lines
37-
)
38-
+ '\n'
39-
+ `The value of the configuration key "${key}" has an invalid type.\n`
40-
+ '\n'
41-
+ `Expected a "${expected}", but got a "${actual}".\n`;
6+
this.message = `The value of the configuration key "${key}" has an invalid type. Expected a "${expected}", but got a "${actual}".`
427
}
438
}

packages/core/src/core/config/utils.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)