trace-error
Advanced tools
Comparing version 0.0.5 to 0.0.6
@@ -23,5 +23,10 @@ 'use strict'; | ||
value: function get(key) { | ||
return this._getProperty('error', {})[key]; | ||
return this._getProperty('error')[key]; | ||
} | ||
}, { | ||
key: 'set', | ||
value: function set(key, value) { | ||
this._getProperty('error')[key] = value; | ||
} | ||
}, { | ||
key: '_defineProperty', | ||
@@ -41,3 +46,6 @@ value: function _defineProperty(key, value) { | ||
get: function get() { | ||
return this._getProperty('error', {}).stack; | ||
return this._getProperty('error').stack; | ||
}, | ||
set: function set(stack) { | ||
this._getProperty('error').stack = stack; | ||
} | ||
@@ -47,3 +55,6 @@ }, { | ||
get: function get() { | ||
return this._getProperty('error', {}).name; | ||
return this._getProperty('error').name; | ||
}, | ||
set: function set(name) { | ||
this._getProperty('error').name = name; | ||
} | ||
@@ -53,3 +64,6 @@ }, { | ||
get: function get() { | ||
return this._getProperty('error', {}).message; | ||
return this._getProperty('error').message; | ||
}, | ||
set: function set(message) { | ||
this._getProperty('error').message = message; | ||
} | ||
@@ -66,8 +80,88 @@ }]); | ||
var error = new (Function.prototype.bind.apply(Error, [null].concat(args)))(); | ||
error.name = this.constructor.name; | ||
this._defineProperty('error', error); | ||
} | ||
_createClass(Exception, [{ | ||
key: 'toJSON', | ||
value: function toJSON() { | ||
if (!Exception.searchPrototype) { | ||
return { stack: this.stack, message: this.message, name: this.name }; | ||
} | ||
var json = {}; | ||
var currProto = this; | ||
var chain = [currProto]; | ||
while (currProto = Object.getPrototypeOf(currProto)) { | ||
chain.push(currProto); | ||
} | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
try { | ||
for (var _iterator = chain[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var proto = _step.value; | ||
var _iteratorNormalCompletion2 = true; | ||
var _didIteratorError2 = false; | ||
var _iteratorError2 = undefined; | ||
try { | ||
for (var _iterator2 = Object.getOwnPropertyNames(proto)[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) { | ||
var key = _step2.value; | ||
if (json.hasOwnProperty(key)) continue; | ||
var desc = Object.getOwnPropertyDescriptor(proto, key); | ||
if (desc) { | ||
var value = desc.get ? desc.get.call(this) : desc.value; | ||
if (proto.isPrototypeOf(value)) { | ||
continue; | ||
} | ||
if (typeof value !== 'function') { | ||
json[key] = value; | ||
} | ||
} | ||
} | ||
} catch (err) { | ||
_didIteratorError2 = true; | ||
_iteratorError2 = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion2 && _iterator2.return) { | ||
_iterator2.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError2) { | ||
throw _iteratorError2; | ||
} | ||
} | ||
} | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} | ||
return json; | ||
} | ||
}]); | ||
return Exception; | ||
}(); | ||
Exception.searchPrototype = false; | ||
exports.default = Exception; | ||
@@ -74,0 +168,0 @@ |
{ | ||
"name": "trace-error", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "Chained errors for JavaScript", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -48,3 +48,3 @@ # TraceError | ||
### ES5/6 Cross-compatibility | ||
Extend the TraceError as such in order to maximize compatibility with ES5 | ||
Extend the TraceError as such in order to maximize compatibility with ES5; additionally, override the `toJSON` as necessary | ||
@@ -56,2 +56,3 @@ ```js | ||
// not ideal | ||
Object.defineProperty(this, 'stack', { | ||
@@ -58,0 +59,0 @@ get: () => super.stack |
@@ -6,18 +6,36 @@ import WeakMap from 'weakmap'; | ||
export default class Exception { | ||
static searchPrototype = false; | ||
get stack() { | ||
return this._getProperty('error', {}).stack; | ||
return this._getProperty('error').stack; | ||
} | ||
set stack(stack) { | ||
this._getProperty('error').stack = stack; | ||
} | ||
get name() { | ||
return this._getProperty('error', {}).name; | ||
return this._getProperty('error').name; | ||
} | ||
set name(name) { | ||
this._getProperty('error').name = name; | ||
} | ||
get message() { | ||
return this._getProperty('error', {}).message; | ||
return this._getProperty('error').message; | ||
} | ||
set message(message) { | ||
this._getProperty('error').message = message; | ||
} | ||
get(key) { | ||
return this._getProperty('error', {})[key]; | ||
return this._getProperty('error')[key]; | ||
} | ||
set(key, value) { | ||
this._getProperty('error')[key] = value; | ||
} | ||
_defineProperty(key, value) { | ||
@@ -35,6 +53,41 @@ const pr = privates.has(this) ? privates.get(this) : {}; | ||
const error = new Error(...args); | ||
error.name = this.constructor.name; | ||
this._defineProperty('error', error); | ||
} | ||
toJSON() { | ||
if (!Exception.searchPrototype) { | ||
return {stack: this.stack, message: this.message, name: this.name}; | ||
} | ||
const json = {}; | ||
let currProto = this; | ||
const chain = [currProto]; | ||
while (currProto = Object.getPrototypeOf(currProto)) { | ||
chain.push(currProto); | ||
} | ||
for (const proto of chain) { | ||
for (const key of Object.getOwnPropertyNames(proto)) { | ||
if (json.hasOwnProperty(key)) continue; | ||
const desc = Object.getOwnPropertyDescriptor(proto, key); | ||
if (desc) { | ||
const value = desc.get ? desc.get.call(this) : desc.value; | ||
if (proto.isPrototypeOf(value)) { | ||
continue; | ||
} | ||
if (typeof value !== 'function') { | ||
json[key] = value; | ||
} | ||
} | ||
} | ||
} | ||
return json; | ||
} | ||
} | ||
Object.setPrototypeOf(Exception.prototype, Error.prototype); |
@@ -1,6 +0,6 @@ | ||
Error: Captured | ||
TraceError: Captured | ||
{ | ||
"test": 5 | ||
} | ||
Error: Test error | ||
TraceError: Test error | ||
Error: Test cause | ||
@@ -7,0 +7,0 @@ { |
@@ -7,53 +7,12 @@ var TraceError = require('../'); | ||
describe('TraceError', function () { | ||
it('should be able to access the property names; overridable with toJSON', function () { | ||
Error.prototype.toJSON = function () { | ||
var alt = {}; | ||
var proto = this; | ||
var chain = [proto]; | ||
it('should be able to access the properties via. toJSON', function () { | ||
const err = new TraceError('sup'); | ||
assert.deepEqual(Object.keys(err.toJSON()).sort(), ['stack', 'name', 'message'].sort()); | ||
TraceError.Exception.searchPrototype = true; | ||
assert.deepEqual(Object.keys(err.toJSON()).sort(), ['stack', 'name', 'message'].sort()); | ||
TraceError.Exception.searchPrototype = false; | ||
}); | ||
while (proto = Object.getPrototypeOf(proto)) { | ||
chain.push(proto); | ||
} | ||
for (var i = 0; i < chain.length; i++) { | ||
var _proto = chain[i]; | ||
var storeKey = function (key) { | ||
if (alt.hasOwnProperty(key)) return; | ||
var desc = Object.getOwnPropertyDescriptor(_proto, key); | ||
if (desc) { | ||
var value = desc.get ? desc.get.call(this) : desc.value; | ||
// avoid the proto keys | ||
if (_proto.isPrototypeOf(value)) { | ||
return; | ||
} | ||
// avoid the base class name | ||
if (key === 'name' && value === 'Error') { | ||
return; | ||
} | ||
if (typeof value !== 'function') { | ||
// assume message is duped in stack | ||
if (key === 'message') return; | ||
if (key === 'stack') { | ||
if (value.indexOf('Error: ') === 0) | ||
value = value.substr(7); | ||
key = 'Error'; | ||
} | ||
alt[key] = value; | ||
} | ||
} | ||
}; | ||
Object.getOwnPropertyNames(_proto).forEach(storeKey, this); | ||
} | ||
return alt; | ||
}; | ||
assert.deepEqual(Object.keys(new TraceError('sup').toJSON()), Object.keys(new Error('sup').toJSON())); | ||
assert.equal(Object.keys(new TraceError('sup').toJSON()).length, 1); | ||
it('should assign the constructor name', function () { | ||
assert.equal(new TraceError('sup').name, 'TraceError'); | ||
}); | ||
@@ -60,0 +19,0 @@ |
22649
525
61