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

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

    I was working on a JSON format for log appenders and ended up here trying to solve a similar problem. After a while, I realized I could just make Node do the work:

    const util = require("util");
    ...
    return JSON.stringify(obj, (name, value) => {
        if (value instanceof Error) {
            return util.format(value);
        } else {
            return value;
        }
    }
    

提交回复
热议问题