diff --git a/HISTORY.md b/HISTORY.md index 4509a59..3d81d26 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,7 +1,6 @@ -Unreleased changes +2.0.1 / 2025-11-20 ================== - * improve toClassName function readability and JSDoc completeness * deps: use tilde notation for dependencies * deps: update statuses to 2.0.2 diff --git a/index.js b/index.js index 82271f6..4c7136d 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) diff --git a/package.json b/package.json index ffcbccb..4b46d62 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "http-errors", "description": "Create HTTP error objects", - "version": "2.0.0", + "version": "2.0.1", "author": "Jonathan Ong (http://jongleberry.com)", "contributors": [ "Alan Plum ", diff --git a/test/test.js b/test/test.js index 7db9f16..8083e3a 100644 --- a/test/test.js +++ b/test/test.js @@ -439,6 +439,28 @@ describe('HTTP Errors', function () { assert((new createError['500']()) instanceof createError.HttpError) }) + 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) + }) + itUtilIsError('should support util.isError()', function () { /* eslint-disable node/no-deprecated-api */ assert(util.isError(createError(404)))