no-json-stringify-error
Avoid generic JSON serialization that can omit native Error details.
Why
Native Error details are non-enumerable, so generic JSON serialization discards diagnostic information.
Fix
Serialize explicit error fields or use an error-aware serializer.
Examples
JSON.stringify({ error: new Error("lost"), ["__proto__"]: { toJSON() { return { message: "kept" }; }, },});JSON.stringify({ error: new Error("lost"), __proto__: { toJSON() { return { message: "kept" }; }, },});const value = new Error("lost");Error = class Other { message = "kept";};JSON.stringify(value);Error = class Other { message = "kept";};const value = new Error();JSON.stringify(value);function probe() { try { throw { id: 1 }; } catch (err) { if (err instanceof Error) return err.message; { const err = new Error("lost"); return JSON.stringify(err); } }}console.log(probe());function probe() { try { throw { id: 1 }; } catch (err) { if (err instanceof Error) return err.message; return JSON.stringify(err); }}console.log(probe());try { f();} catch (err) { JSON.stringify({ error: err });}try { f();} catch (err) { JSON.stringify({ error: err instanceof Error ? err.message : String(err) });}JSON.stringify({ error: new Error("lost"), toJSON: 42 });JSON.stringify({ error: new Error("lost"), toJSON() { return { message: "kept" }; },});const result = JSON.stringify(new Error("lost"));JSON.stringify = (value) => "kept:" + value.message;JSON.stringify = (value) => "kept:" + value.message;JSON.stringify(new Error("lost"));const error = new Error("lost", { cause: "kept" });JSON.stringify(error);const error = new Error("lost", { cause: "kept" });JSON.stringify(error.cause);const key = "stringify";JSON[key](new Error("lost"));const key = "message";const error = new Error("lost");JSON.stringify(error[key]);