From a0c7431787618ee8d734c4571729e2f219141e3f Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Tue, 31 May 2016 15:11:57 +0200 Subject: [PATCH] Inherit the name property in the error classes --- lib/customErrors.js | 21 +++++++++++---------- test/custom_errors.spec.js | 8 ++++---- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/lib/customErrors.js b/lib/customErrors.js index 0b192f78df..bdf2feda0c 100644 --- a/lib/customErrors.js +++ b/lib/customErrors.js @@ -4,11 +4,6 @@ var util = require('util'); function AbortError (obj) { Error.captureStackTrace(this, this.constructor); - Object.defineProperty(this, 'name', { - value: 'AbortError', - configurable: true, - writable: true - }); Object.defineProperty(this, 'message', { value: obj.message || '', configurable: true, @@ -21,11 +16,6 @@ function AbortError (obj) { function AggregateError (obj) { Error.captureStackTrace(this, this.constructor); - Object.defineProperty(this, 'name', { - value: 'AggregateError', - configurable: true, - writable: true - }); Object.defineProperty(this, 'message', { value: obj.message || '', configurable: true, @@ -39,6 +29,17 @@ function AggregateError (obj) { util.inherits(AbortError, Error); util.inherits(AggregateError, AbortError); +Object.defineProperty(AbortError.prototype, 'name', { + value: 'AbortError', + // configurable: true, + writable: true +}); +Object.defineProperty(AggregateError.prototype, 'name', { + value: 'AggregateError', + // configurable: true, + writable: true +}); + module.exports = { AbortError: AbortError, AggregateError: AggregateError diff --git a/test/custom_errors.spec.js b/test/custom_errors.spec.js index d7c6ebb602..90a5aaeb66 100644 --- a/test/custom_errors.spec.js +++ b/test/custom_errors.spec.js @@ -24,11 +24,11 @@ describe('errors', function () { assert.strictEqual(e.message, 'hello world'); assert.strictEqual(e.name, 'weird'); assert.strictEqual(e.property, true); - assert.strictEqual(Object.keys(e).length, 1); + assert.strictEqual(Object.keys(e).length, 2); assert(e instanceof Error); assert(e instanceof errors.AbortError); assert(delete e.name); - assert.strictEqual(e.name, 'Error'); + assert.strictEqual(e.name, 'AbortError'); }); it('should change name and message', function () { @@ -65,12 +65,12 @@ describe('errors', function () { assert.strictEqual(e.message, 'hello world'); assert.strictEqual(e.name, 'weird'); assert.strictEqual(e.property, true); - assert.strictEqual(Object.keys(e).length, 1); + assert.strictEqual(Object.keys(e).length, 2); assert(e instanceof Error); assert(e instanceof errors.AggregateError); assert(e instanceof errors.AbortError); assert(delete e.name); - assert.strictEqual(e.name, 'Error'); + assert.strictEqual(e.name, 'AggregateError'); }); it('should change name and message', function () {