From d2fe4f35985df3430f60726bccff66c80f5dee48 Mon Sep 17 00:00:00 2001 From: Nathan Lepori Date: Wed, 21 Jun 2023 13:18:11 +0200 Subject: [PATCH 1/2] Improved interoperability with ES6 classes --- index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index c425f1e..f34fb4c 100644 --- a/index.js +++ b/index.js @@ -111,7 +111,9 @@ function createError () { function createHttpErrorConstructor () { function HttpError () { - throw new TypeError('cannot construct abstract class') + if (this.constructor === HttpError) { + throw new TypeError('cannot construct abstract class') + } } inherits(HttpError, Error) From 6d8791e9f0ed8c98a3306a7c55c063d55d44a3f1 Mon Sep 17 00:00:00 2001 From: Nathan Lepori Date: Sat, 18 Nov 2023 15:20:45 +0100 Subject: [PATCH 2/2] Added test for ES6 classes interoperability --- test/test.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/test.js b/test/test.js index 0059d01..9fa0956 100644 --- a/test/test.js +++ b/test/test.js @@ -406,4 +406,26 @@ describe('HTTP Errors', function () { assert(util.isError(new createError['500']())) /* eslint-enable node/no-deprecated-api */ }) + + it('should interoperate with ES6 classes', function () { + class MyHttpError extends createError.HttpError { + constructor () { + super() + this.name = 'MyHttpError' + this.message = 'ES6 class HTTPError' + this.status = 404 + this.myProp = 'test' + } + } + + // Testing PR#99: This line should not throw TypeError('cannot construct abstract class') + const err = new MyHttpError() + assert.strictEqual(err.name, 'MyHttpError') + assert.strictEqual(err.message, 'ES6 class HTTPError') + assert.strictEqual(err.status, 404) + assert.strictEqual(err.myProp, 'test') + /* eslint-disable-next-line node/no-deprecated-api */ + assert.strictEqual(util.isError(err), true) + assert.strictEqual(createError.isHttpError(err), true) + }) })