Skip to content

Commit a0c7431

Browse files
author
Ruben Bridgewater
committed
Inherit the name property in the error classes
1 parent c1f7755 commit a0c7431

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

lib/customErrors.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@ var util = require('util');
44

55
function AbortError (obj) {
66
Error.captureStackTrace(this, this.constructor);
7-
Object.defineProperty(this, 'name', {
8-
value: 'AbortError',
9-
configurable: true,
10-
writable: true
11-
});
127
Object.defineProperty(this, 'message', {
138
value: obj.message || '',
149
configurable: true,
@@ -21,11 +16,6 @@ function AbortError (obj) {
2116

2217
function AggregateError (obj) {
2318
Error.captureStackTrace(this, this.constructor);
24-
Object.defineProperty(this, 'name', {
25-
value: 'AggregateError',
26-
configurable: true,
27-
writable: true
28-
});
2919
Object.defineProperty(this, 'message', {
3020
value: obj.message || '',
3121
configurable: true,
@@ -39,6 +29,17 @@ function AggregateError (obj) {
3929
util.inherits(AbortError, Error);
4030
util.inherits(AggregateError, AbortError);
4131

32+
Object.defineProperty(AbortError.prototype, 'name', {
33+
value: 'AbortError',
34+
// configurable: true,
35+
writable: true
36+
});
37+
Object.defineProperty(AggregateError.prototype, 'name', {
38+
value: 'AggregateError',
39+
// configurable: true,
40+
writable: true
41+
});
42+
4243
module.exports = {
4344
AbortError: AbortError,
4445
AggregateError: AggregateError

test/custom_errors.spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ describe('errors', function () {
2424
assert.strictEqual(e.message, 'hello world');
2525
assert.strictEqual(e.name, 'weird');
2626
assert.strictEqual(e.property, true);
27-
assert.strictEqual(Object.keys(e).length, 1);
27+
assert.strictEqual(Object.keys(e).length, 2);
2828
assert(e instanceof Error);
2929
assert(e instanceof errors.AbortError);
3030
assert(delete e.name);
31-
assert.strictEqual(e.name, 'Error');
31+
assert.strictEqual(e.name, 'AbortError');
3232
});
3333

3434
it('should change name and message', function () {
@@ -65,12 +65,12 @@ describe('errors', function () {
6565
assert.strictEqual(e.message, 'hello world');
6666
assert.strictEqual(e.name, 'weird');
6767
assert.strictEqual(e.property, true);
68-
assert.strictEqual(Object.keys(e).length, 1);
68+
assert.strictEqual(Object.keys(e).length, 2);
6969
assert(e instanceof Error);
7070
assert(e instanceof errors.AggregateError);
7171
assert(e instanceof errors.AbortError);
7272
assert(delete e.name);
73-
assert.strictEqual(e.name, 'Error');
73+
assert.strictEqual(e.name, 'AggregateError');
7474
});
7575

7676
it('should change name and message', function () {

0 commit comments

Comments
 (0)