Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved interoperability with ES6 classes #99

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})