error-to-json
Returns a JSON representation of an error (handles nested errors and calls nested toJSONs)
Installation
npm i --save error-to-json
Usage
Supports both ESM and CommonJS
// esm// commonjsconst errorToJSON =
Example: toJSON
var model = _json: x: 1 y: 2 {/*...*/} { return this_json }var err = 'boom'errdata = err: 'boom2' model: modelvar json = /*{ "name": "Error", "message": "boom", "stack": "Error: boom\n ....", "data": { "err": { // nested error was also converted to json "name": "TypeError", "message": "boom2", "stack": "TypeError: boom2\n ...." }, "model": { // toJSON was called! "x": 1, "y": 2 } }} */
Example: Parse
var err = /* > err [ Error: boom2 ] > err.stack `Error: boom2 at ... at ...`*/ var err2 = /* > err2 [ Error: boom ] > err2.stack 'Error: boom'*/
License
MIT