When we try to JSON.stringify()
an error object, we will get an empty object {}
. This is because the error object has no enumerable properties.
js
JSON.stringify(new Error("error")); // '{}'
To include the error message and stack trace, we can use a replacer function.
js
function errorReplacer(key, value) {
if (value instanceof Error) {
return {
: value.name,
name: value.message,
message: value.stack,
stack
};
}return value;
}
JSON.stringify(new Error("error"), errorReplacer); // '{"name":"Error","message":"error","stack":"Error: error\\n at <anonymous>:12:16"}'
JSON.stringify(new SyntaxError("syntaxError"), errorReplacer); // '{"name":"SyntaxError","message":"syntaxError","stack":"SyntaxError: syntaxError\\n at <anonymous>:13:16"}'