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

Remove inherits and setprototypeof dependenciess, use real classes #111

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
104 changes: 25 additions & 79 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,30 @@
*/

var deprecate = require('depd')('http-errors')
var setPrototypeOf = require('setprototypeof')
var statuses = require('statuses')
var inherits = require('inherits')
var toIdentifier = require('toidentifier')

class HttpError extends Error {
constructor (status, message) {
if (new.target === HttpError) {
throw new TypeError('cannot construct abstract class')
}
super(message)
this.status = this.statusCode = status
}
}

/**
* Module exports.
* @public
*/

module.exports = createError
module.exports.HttpError = createHttpErrorConstructor()
module.exports.isHttpError = createIsHttpErrorFunction(module.exports.HttpError)
module.exports.HttpError = HttpError
module.exports.isHttpError = createIsHttpErrorFunction(HttpError)

// Populate exports for all constructors
populateConstructorExports(module.exports, statuses.codes, module.exports.HttpError)
populateConstructorExports(module.exports, statuses.codes, HttpError)

/**
* Get the code class of a status code.
Expand Down Expand Up @@ -86,7 +94,6 @@ function createError () {
err = HttpError
? new HttpError(msg)
: new Error(msg || statuses.message[status])
Error.captureStackTrace(err, createError)
}

if (!HttpError || !(err instanceof HttpError) || err.status !== status) {
Expand All @@ -104,21 +111,6 @@ function createError () {
return err
}

/**
* Create HTTP error abstract base class.
* @private
*/

function createHttpErrorConstructor () {
function HttpError () {
throw new TypeError('cannot construct abstract class')
}

inherits(HttpError, Error)

return HttpError
}

/**
* Create a constructor for a client error.
* @private
Expand All @@ -127,39 +119,16 @@ function createHttpErrorConstructor () {
function createClientErrorConstructor (HttpError, name, code) {
var className = toClassName(name)

function ClientError (message) {
// create the error object
var msg = message != null ? message : statuses.message[code]
var err = new Error(msg)

// capture a stack trace to the construction point
Error.captureStackTrace(err, ClientError)

// adjust the [[Prototype]]
setPrototypeOf(err, ClientError.prototype)

// redefine the error message
Object.defineProperty(err, 'message', {
enumerable: true,
configurable: true,
value: msg,
writable: true
})

// redefine the error name
Object.defineProperty(err, 'name', {
enumerable: false,
configurable: true,
value: className,
writable: true
})

return err
class ClientError extends HttpError {
constructor (message) {
const msg = message != null ? message : statuses.message[code]
super(code, msg)
}
}

inherits(ClientError, HttpError)
nameFunc(ClientError, className)

ClientError.prototype.name = className
ClientError.prototype.status = code
ClientError.prototype.statusCode = code
ClientError.prototype.expose = true
Expand Down Expand Up @@ -196,39 +165,16 @@ function createIsHttpErrorFunction (HttpError) {
function createServerErrorConstructor (HttpError, name, code) {
var className = toClassName(name)

function ServerError (message) {
// create the error object
var msg = message != null ? message : statuses.message[code]
var err = new Error(msg)

// capture a stack trace to the construction point
Error.captureStackTrace(err, ServerError)

// adjust the [[Prototype]]
setPrototypeOf(err, ServerError.prototype)

// redefine the error message
Object.defineProperty(err, 'message', {
enumerable: true,
configurable: true,
value: msg,
writable: true
})

// redefine the error name
Object.defineProperty(err, 'name', {
enumerable: false,
configurable: true,
value: className,
writable: true
})

return err
class ServerError extends HttpError {
constructor (message) {
var msg = message != null ? message : statuses.message[code]
super(code, msg)
}
}

inherits(ServerError, HttpError)
nameFunc(ServerError, className)

ServerError.prototype.name = className
ServerError.prototype.status = code
ServerError.prototype.statusCode = code
ServerError.prototype.expose = false
Expand Down
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
"repository": "jshttp/http-errors",
"dependencies": {
"depd": "2.0.0",
"inherits": "2.0.4",
"setprototypeof": "1.2.0",
"statuses": "2.0.1",
"toidentifier": "1.0.1"
},
Expand Down
18 changes: 18 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var assert = require('assert')
var util = require('util')

var createError = require('..')
const { HttpError } = createError

describe('createError(status)', function () {
it('should create error object', function () {
Expand Down Expand Up @@ -407,3 +408,20 @@ describe('HTTP Errors', function () {
/* eslint-enable node/no-deprecated-api */
})
})

describe('Inheritance', function () {
it('should support subclasses', function () {
class MyError extends HttpError {
}
const err = new MyError()
assert.ok(err instanceof HttpError, 'subclass instances are instances of HttpError')
})

it('should set status and message', function () {
class MyError extends HttpError {
}
const err = new MyError(202, 'My Error')
assert.strictEqual(err.status, 202)
assert.strictEqual(err.message, 'My Error')
})
})