Skip to content

Commit 8d55d6b

Browse files
authoredJun 16, 2024··
fix: toJSON recursive error serialization (fix #5848) (#5884)
1 parent 84a4fdb commit 8d55d6b

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed
 

Diff for: ‎packages/utils/src/error.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export function serializeError(val: any, seen = new WeakMap()): any {
4545
if (typeof val.asymmetricMatch === 'function')
4646
return `${val.toString()} ${format(val.sample)}`
4747
if (typeof val.toJSON === 'function')
48-
return val.toJSON()
48+
return serializeError(val.toJSON(), seen)
4949

5050
if (seen.has(val))
5151
return seen.get(val)

Diff for: ‎test/core/test/serialize.test.ts

+28
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,32 @@ describe('error serialize', () => {
168168
message: 'test',
169169
})
170170
})
171+
172+
it('should serialize when toJSON does not return JSON-compatible object', () => {
173+
class TestClass {}
174+
175+
const error = {
176+
key: 'value',
177+
// Still not serialized, thus second round of serialization is needed
178+
toJSON() {
179+
return {
180+
key: 'value',
181+
obj: {
182+
fn: () => {},
183+
class: TestClass,
184+
},
185+
}
186+
},
187+
}
188+
189+
const serialized = serializeError(error)
190+
191+
expect(serialized).toEqual({
192+
key: 'value',
193+
obj: {
194+
fn: 'Function<fn>',
195+
class: 'Function<TestClass>',
196+
},
197+
})
198+
})
171199
})

0 commit comments

Comments
 (0)
Please sign in to comment.