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

后端 未结 11 2092
心在旅途
心在旅途 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:44

    There is a great Node.js package for that: serialize-error.

    npm install serialize-error
    

    It handles well even nested Error objects.

    import {serializeError} from 'serialize-error';
    
    JSON.stringify(serializeError(error));
    

    Docs: https://www.npmjs.com/package/serialize-error

提交回复
热议问题