Skip to content

Commit bae2552

Browse files
authored
fix: correctly serialize Error objects (#18)
CDM-12791
1 parent 8ed9c64 commit bae2552

File tree

3 files changed

+97
-24
lines changed

3 files changed

+97
-24
lines changed

src/error.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -466,9 +466,9 @@ Object.defineProperty(Error.prototype, 'toJSON', {
466466

467467
if (this.hash != null) json.hash = this.hash;
468468
if (this.bundle != null) json.bundle = this.bundle;
469-
if (Array.isArray(this.suberrors)) json.suberrors = this.suberrors.map((item: Error) => item.toJSON());
469+
if (Array.isArray(this.suberrors)) json.suberrors = this.suberrors.map((item: Error) => item);
470470
if (this.external != null) json.external = this.external;
471-
if (this.imtInternalError) json.imtInternalError = this.imtInternalError.toJSON();
471+
if (this.imtInternalError) json.imtInternalError = this.imtInternalError;
472472
if (this.imtExceptionHash) json.imtExceptionHash = this.imtExceptionHash;
473473

474474
return json;

test/error.spec.ts

+89-21
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as assert from 'assert';
22
import { DataError, InvalidAccessTokenError, UnknownError } from '../src/error';
33

44
describe('Error', () => {
5-
it('UnknownError', (done) => {
5+
it('UnknownError', () => {
66
let e = new UnknownError(new Error('Error message.'));
77

88
assert.equal(e.message, 'Error message.');
@@ -25,62 +25,130 @@ describe('Error', () => {
2525

2626
assert.equal(err.message, 'Error message.');
2727
assert.equal(err.something, true);
28-
29-
done();
3028
});
3129

32-
it('DataError', (done) => {
30+
it('DataError', () => {
3331
const e = new DataError('Some message.');
3432

3533
assert.equal(e.name, 'DataError');
3634
assert.equal(e.message, 'Some message.');
3735
assert.ok(/^DataError: Some message.\n/.test(e.stack!));
38-
39-
done();
4036
});
4137

42-
it('InvalidAccessTokenError', (done) => {
38+
it('InvalidAccessTokenError', () => {
4339
const e = new InvalidAccessTokenError('Some message.');
4440

4541
assert.equal(e.name, 'InvalidAccessTokenError');
4642
assert.equal(e.message, 'Some message.');
4743
assert.ok(/^InvalidAccessTokenError: Some message.\n/.test(e.stack!));
44+
});
45+
46+
it('JSON serialization with sub-error', () => {
47+
const error: any = new DataError('Some message.');
48+
const subError = new TypeError('Type message.');
49+
50+
error.hash = 'im-hash';
51+
error.external = true;
52+
error.something = 'donotserializeme';
53+
error.suberrors = [subError];
54+
error.imtInternalError = subError;
4855

49-
done();
56+
assert.deepStrictEqual(JSON.parse(JSON.stringify(error)), {
57+
name: 'DataError',
58+
message: 'Some message.',
59+
stack: error.stack,
60+
hash: 'im-hash',
61+
external: true,
62+
suberrors: [
63+
{
64+
name: 'TypeError',
65+
message: 'Type message.',
66+
stack: subError.stack,
67+
},
68+
],
69+
imtInternalError: {
70+
name: 'TypeError',
71+
message: 'Type message.',
72+
stack: subError.stack,
73+
},
74+
imtExceptionHash: 'im-hash',
75+
});
5076
});
5177

52-
it('JSON serialization', (done) => {
53-
const e: any = new DataError('Some message.');
54-
const ee = new TypeError('Type message.');
78+
it('JSON serialization with sub-error and sub-sub-error', () => {
79+
const error: any = new DataError('Some message.');
80+
const subError = new TypeError('Type message.');
81+
const subSubError = new TypeError('Type message.');
5582

56-
e.hash = 'im-hash';
57-
e.external = true;
58-
e.something = 'donotserializeme';
59-
e.suberrors = [ee];
60-
e.imtInternalError = ee;
83+
error.hash = 'im-hash';
84+
error.external = true;
85+
error.something = 'donotserializeme';
86+
error.suberrors = [subError];
87+
error.imtInternalError = subError;
88+
subError.suberrors = [subSubError];
6189

62-
assert.deepStrictEqual(e.toJSON(), {
90+
assert.deepStrictEqual(JSON.parse(JSON.stringify(error)), {
6391
name: 'DataError',
6492
message: 'Some message.',
65-
stack: e.stack,
93+
stack: error.stack,
6694
hash: 'im-hash',
6795
external: true,
6896
suberrors: [
6997
{
7098
name: 'TypeError',
7199
message: 'Type message.',
72-
stack: ee.stack,
100+
stack: subError.stack,
101+
suberrors: [
102+
{
103+
name: 'TypeError',
104+
message: 'Type message.',
105+
stack: subSubError.stack,
106+
},
107+
],
73108
},
74109
],
75110
imtInternalError: {
76111
name: 'TypeError',
77112
message: 'Type message.',
78-
stack: ee.stack,
113+
stack: subError.stack,
114+
suberrors: [
115+
{
116+
name: 'TypeError',
117+
message: 'Type message.',
118+
stack: subSubError.stack,
119+
},
120+
],
79121
},
80122
imtExceptionHash: 'im-hash',
81123
});
124+
});
125+
126+
it('JSON serialization with sub-error that is not an Error class instance', () => {
127+
const error: any = new DataError('Some message.');
128+
const subError = { message: 'Type message.' };
129+
130+
error.hash = 'im-hash';
131+
error.external = true;
132+
error.something = 'donotserializeme';
133+
error.suberrors = [subError];
134+
error.imtInternalError = subError;
82135

83-
done();
136+
assert.deepStrictEqual(JSON.parse(JSON.stringify(error)), {
137+
name: 'DataError',
138+
message: 'Some message.',
139+
stack: error.stack,
140+
hash: 'im-hash',
141+
external: true,
142+
suberrors: [
143+
{
144+
message: 'Type message.',
145+
},
146+
],
147+
imtInternalError: {
148+
message: 'Type message.',
149+
},
150+
imtExceptionHash: 'im-hash',
151+
});
84152
});
85153

86154
it('should keep hash and imtExceptionHash in sync', () => {

tsconfig.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,10 @@
1919
"declarationMap": true,
2020
"sourceMap": true
2121
},
22-
"include": ["src/**/*.ts"]
22+
"include": ["src/**/*.ts"],
23+
"references": [
24+
{
25+
"path": "./tsconfig.spec.json"
26+
}
27+
]
2328
}

0 commit comments

Comments
 (0)