Skip to content

Commit 09c54c6

Browse files
committed
fix: improve error handling and add more tests
1 parent 3b2a19e commit 09c54c6

File tree

2 files changed

+49
-16
lines changed

2 files changed

+49
-16
lines changed

src/ParseError.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,25 @@ class ParseError extends Error {
2020
constructor(code, message) {
2121
super(message);
2222
this.code = code;
23+
24+
const stringify = obj => {
25+
let log = '';
26+
for (const k in obj) {
27+
log += `${obj[k]} `;
28+
}
29+
return log.trim();
30+
};
31+
2332
Object.defineProperty(this, 'message', {
2433
enumerable: true,
2534
value:
2635
typeof message === 'string'
2736
? message
2837
: typeof message === 'object' &&
2938
typeof message.toString === 'function' &&
30-
message.toString() !== '[object Object]'
39+
!message.toString().includes('[object Object]')
3140
? message.toString()
32-
: JSON.stringify(message),
41+
: stringify(message),
3342
});
3443
}
3544

src/__tests__/ParseError-test.js

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,50 @@ describe('ParseError', () => {
2828
});
2929
});
3030

31-
it('message must be a string', () => {
32-
/**
33-
* error as object
34-
*/
35-
const someRandomError = { code: 420, message: 'time to chill' };
31+
it('message can be a string', () => {
32+
const someRandomError = 'oh no';
33+
34+
const error = new ParseError(1337, someRandomError);
35+
36+
expect(JSON.parse(JSON.stringify(error))).toEqual({
37+
message: someRandomError,
38+
code: 1337,
39+
});
40+
});
41+
42+
it('message can be an object passed trough some external dependency', () => {
43+
const someRandomError = { code: '420', message: 'time to chill', status: '🎮' };
44+
3645
const error = new ParseError(1337, someRandomError);
46+
3747
expect(JSON.parse(JSON.stringify(error))).toEqual({
38-
message: JSON.stringify(someRandomError),
48+
message: '420 time to chill 🎮',
3949
code: 1337,
4050
});
51+
});
4152

42-
/**
43-
* error as an Error instance
44-
*/
45-
const someRandomError2 = new Error('time to relax');
46-
const error2 = new ParseError(420, someRandomError2);
53+
it('message can be an Error instance *receiving a string* passed trough some external dependency', () => {
54+
const someRandomError = new Error('good point');
4755

48-
expect(JSON.parse(JSON.stringify(error2))).toEqual({
49-
message: 'Error: time to relax',
50-
code: 420,
56+
const error = new ParseError(1337, someRandomError);
57+
58+
expect(JSON.parse(JSON.stringify(error))).toEqual({
59+
message: 'Error: good point',
60+
code: 1337,
61+
});
62+
});
63+
64+
it('message can be an Error instance *receiving an object* passed trough some external dependency', () => {
65+
const someRandomErrorWrong = new Error({
66+
code: 'WRONG',
67+
message: 'this is not how errors should be handled',
68+
});
69+
70+
const error = new ParseError(1337, someRandomErrorWrong);
71+
72+
expect(JSON.parse(JSON.stringify(error))).toEqual({
73+
message: '', // <-- Yeah because we can't parse errors used like that
74+
code: 1337,
5175
});
5276
});
5377
});

0 commit comments

Comments
 (0)