serialize-javascript icon indicating copy to clipboard operation
serialize-javascript copied to clipboard

`Error` objects are serialized to `{}`

Open wujekbogdan opened this issue 3 years ago • 4 comments

When I do:

import serializeJavascript from 'serialize-javascript';
const serialized = serializeJavascript({  error: new Error('message') });

console.log(serialized);

I'm receiving:

"{\"error\":{}}"

Is it intentional or is it a bug?

wujekbogdan avatar Dec 07 '21 14:12 wujekbogdan

I have reviewed the code, and currently, only deal with the following types:

RegExp,
Date,
Map,
Set,
Array,
URL,
function,
undefined,
number,
bigint

Error Object was not handled.

If you feel the need, you can try to add it, and I will also try it too.

SidStraw avatar Dec 11 '21 17:12 SidStraw

patches welcome!

okuryu avatar Dec 13 '21 00:12 okuryu

I guess it happens because Error object properties are not enumerable. There's a lib serialize-error that solves this issue.

wujekbogdan avatar Dec 13 '21 11:12 wujekbogdan

Hello, @okuryu @wujekbogdan

I have added a patch about the support Error object. #136

PTAL, thx.

SidStraw avatar Dec 16 '21 17:12 SidStraw