Is it not possible to stringify an Error using JSON.stringify?

后端 未结 11 2121
心在旅途
心在旅途 2020-11-22 07:54

Reproducing the problem

I\'m running into an issue when trying to pass error messages around using web sockets. I can replicate the issue I am facing using J

11条回答
  •  遇见更好的自我
    2020-11-22 08:26

    As no one is talking about the why part, I'm gonna answer it.

    Why this JSON.stringify returns an empty object?

    > JSON.stringify(error);
    '{}'
    

    Answer

    From the document of JSON.stringify(),

    For all the other Object instances (including Map, Set, WeakMap and WeakSet), only their enumerable properties will be serialized.

    and Error object doesn't have its enumerable properties, that's why it prints an empty object.

提交回复
热议问题