serialised-error
Advanced tools
Comparing version
15
index.js
@@ -7,2 +7,4 @@ var stacktrace = require('stack-trace'), | ||
SerialisedError = function (err, decorate) { | ||
var now = new Date(); // to hold date here before any more time is lost | ||
// If the function is called without the `new` operator, then we do it on behalf of the callee | ||
@@ -14,5 +16,6 @@ if (!(this instanceof SerialisedError)) { | ||
// Iterate on user-defined properties of error and mix in the default non ennumerable properties | ||
(err instanceof Error) && Object.keys(err).concat(['name', 'message', 'stack']).forEach(function (key) { | ||
this[key] = err[key]; | ||
}, this); | ||
(typeof err === 'object') && (err !== null) && | ||
Object.keys(err).concat(['name', 'message', 'stack']).forEach(function (key) { | ||
this[key] = err[key]; | ||
}, this); | ||
@@ -23,3 +26,3 @@ // add additional meta information | ||
this.id = uuid.v4(); | ||
this.timestamp = Date.now(); | ||
this.timestamp = now.getTime(); | ||
this.stacktrace = stacktrace.parse(this); | ||
@@ -29,6 +32,2 @@ } | ||
SerialisedError.prototype.toJSON = function () { | ||
return JSON.stringify(this); | ||
}; | ||
module.exports = SerialisedError; |
{ | ||
"name": "serialised-error", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"description": "Serialises error object to normal object", | ||
@@ -27,10 +27,10 @@ "main": "index.js", | ||
"expect.js": "^0.3.1", | ||
"jshint": "^2.8.0", | ||
"mocha": "^2.2.5" | ||
"jshint": "^2.9.1", | ||
"mocha": "^2.4.5" | ||
}, | ||
"dependencies": { | ||
"node-uuid": "^1.4.3", | ||
"object-hash": "^0.8.0", | ||
"node-uuid": "^1.4.7", | ||
"object-hash": "^1.1.2", | ||
"stack-trace": "0.0.9" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
# Serialise Error | ||
# Serialised Error | ||
@@ -11,3 +11,3 @@ This module attempts to convert an error object into a regular JavaScript object. This is useful if an error object has | ||
// assuming you have an error | ||
// assuming you have an error | ||
var someError = new Error("This is a test error"); | ||
@@ -19,5 +19,21 @@ | ||
// convert the serialised error to JSON | ||
console.log(serialisedError.toJSON()); | ||
console.log(JSON.parse(serialisedError)); | ||
// this outputs: | ||
// {"name": "Error", "message": "This is a test error", "stack": "Error\n at ..."} | ||
``` | ||
## Adding additional meta information to error | ||
Passing a second argument as `true` to the `SerialisedError` constructor adds the following keys to the serialised object. | ||
| Property | Description | | ||
| --------------- | ----------- | | ||
| `checksum` | a `SHA1` checksum of the error that is constant for same name, message and stack | | ||
| `id` | a random `UUID` (v4) of the error | | ||
| `timestamp` | the time when the error was serialised | | ||
| `timestampISO` | the time (in ISO format) when the error was serialised | | ||
| `stacktrace` | a prettified array of stack traces | | ||
## Installation | ||
@@ -27,2 +43,2 @@ | ||
npm install serialised-error; | ||
``` | ||
``` |
18026
4.24%42
68%47
-2.08%+ Added
- Removed
Updated
Updated