serialize-error
Advanced tools
Comparing version 2.1.0 to 3.0.0
69
index.js
'use strict'; | ||
// Make a value ready for JSON.stringify() / process.send() | ||
module.exports = function (value) { | ||
if (typeof value === 'object') { | ||
return destroyCircular(value, []); | ||
} | ||
const destroyCircular = (from, seen) => { | ||
const to = Array.isArray(from) ? [] : {}; | ||
// People sometimes throw things besides Error objects, so... | ||
if (typeof value === 'function') { | ||
// JSON.stringify discards functions. We do to, unless a function is thrown directly. | ||
return '[Function: ' + (value.name || 'anonymous') + ']'; | ||
} | ||
return value; | ||
}; | ||
// https://www.npmjs.com/package/destroy-circular | ||
function destroyCircular(from, seen) { | ||
var to; | ||
if (Array.isArray(from)) { | ||
to = []; | ||
} else { | ||
to = {}; | ||
} | ||
seen.push(from); | ||
Object.keys(from).forEach(function (key) { | ||
var value = from[key]; | ||
// TODO: Use `Object.entries() when targeting Node.js 8 | ||
for (const key of Object.keys(from)) { | ||
const value = from[key]; | ||
if (typeof value === 'function') { | ||
return; | ||
continue; | ||
} | ||
@@ -39,26 +18,36 @@ | ||
to[key] = value; | ||
return; | ||
continue; | ||
} | ||
if (seen.indexOf(from[key]) === -1) { | ||
to[key] = destroyCircular(from[key], seen.slice(0)); | ||
return; | ||
if (!seen.includes(from[key])) { | ||
to[key] = destroyCircular(from[key], seen.slice()); | ||
continue; | ||
} | ||
to[key] = '[Circular]'; | ||
}); | ||
} | ||
if (typeof from.name === 'string') { | ||
to.name = from.name; | ||
const commonProperties = ['name', 'message', 'stack', 'code']; | ||
for (const property of commonProperties) { | ||
if (typeof from[property] === 'string') { | ||
to[property] = from[property]; | ||
} | ||
} | ||
if (typeof from.message === 'string') { | ||
to.message = from.message; | ||
return to; | ||
}; | ||
module.exports = value => { | ||
if (typeof value === 'object') { | ||
return destroyCircular(value, []); | ||
} | ||
if (typeof from.stack === 'string') { | ||
to.stack = from.stack; | ||
// People sometimes throw things besides Error objects… | ||
if (typeof value === 'function') { | ||
// JSON.stringify discards functions. We do too, unless a function is thrown directly. | ||
return `[Function: ${(value.name || 'anonymous')}]`; | ||
} | ||
return to; | ||
} | ||
return value; | ||
}; |
{ | ||
"name": "serialize-error", | ||
"version": "2.1.0", | ||
"description": "Serialize an error into a plain object", | ||
"license": "MIT", | ||
"repository": "sindresorhus/serialize-error", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=0.10.0" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"error", | ||
"err", | ||
"serialize", | ||
"stringify", | ||
"object", | ||
"obj", | ||
"convert", | ||
"process", | ||
"send" | ||
], | ||
"devDependencies": { | ||
"ava": "*", | ||
"xo": "^0.16.0" | ||
} | ||
"name": "serialize-error", | ||
"version": "3.0.0", | ||
"description": "Serialize an error into a plain object", | ||
"license": "MIT", | ||
"repository": "sindresorhus/serialize-error", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=6" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"error", | ||
"err", | ||
"serialize", | ||
"stringify", | ||
"object", | ||
"convert", | ||
"process", | ||
"send" | ||
], | ||
"devDependencies": { | ||
"ava": "^0.25.0", | ||
"xo": "^0.23.0" | ||
} | ||
} |
@@ -11,3 +11,3 @@ # serialize-error [![Build Status](https://travis-ci.org/sindresorhus/serialize-error.svg?branch=master)](https://travis-ci.org/sindresorhus/serialize-error) | ||
``` | ||
$ npm install --save serialize-error | ||
$ npm install serialize-error | ||
``` | ||
@@ -26,3 +26,3 @@ | ||
console.log(serializeError(error)); | ||
//=> {name: 'Error', message: 'unicorn', stack: 'Error: unicorn\n at Object.<anonymous> ...'} | ||
//=> {name: 'Error', message: 'unicorn', stack: 'Error: unicorn\n at Object.<anonymous> …'} | ||
``` | ||
@@ -29,0 +29,0 @@ |
Sorry, the diff of this file is not supported yet
3459
39