Skip to content

Allow HttpError extension by ES6 classes #98

Description

@nathanlepori

To allow better interoperability with ES6 classes (which are forced to call the super() constructor) it is better to add the following condition to the HttpError constructor:

if (this.constructor === HttpError) {
    throw new TypeError('cannot construct abstract class')
}

This way the class can be extended with a concrete class like this:

class MyHttpError extends HttpError {
    constructor() {
        super();
        ...
    }
}

A variant using ES6 classes as base class is shown here but the same works with old school constructor functions as well.

Activity

  1. jonchurch commented on Mar 8, 2024

    @jonchurch
    Member

    moving this comment and repro from another issue #101 :

    #101 (comment)

    I was surprised I couldn't subclass errors. I would have expected my ValidationError class would support an instanceof check against itself.

    const { BadRequest } = require('http-errors')
    const assert = require('assert')
    
    
    class ValidationError extends BadRequest {}
    
    const err = new ValidationError()
    console.log(err.name)
    // "BadRequestError"
    
    // err should be an instance of ValidationError
    assert(err instanceof ValidationError)
    

    https://runkit.com/cdignam-segment/6536e492c20c690008824a73

  2. jonchurch commented on Mar 9, 2024

    @jonchurch
    Member

    There's two different concepts in this issue.

    Extending HttpError is not possible currently because of the throw to prevent instantiating the abstract class.

    const createErrors = require('http-errors')
    
    class NewBase extends createErrors.HttpError // this will throw a type error

    Extending error classes like NotFound do not wire up the prototype properly to be able to check insanceof on it.

    const createErrors = require('http-errors')
    
    class ValidationError extends createErrors.BadRequest {}
    
    assert(err instanceof ValidationError) // false
  3. mahmoodhamdi commented on Mar 29, 2026

    @mahmoodhamdi

    I ran into this limitation when trying to create custom error subclasses in my Express app. The current workaround is verbose:

    class ValidationError {
      constructor(message, fields) {
        const err = createError(422, message)
        err.fields = fields
        return err
      }
    }

    The new.target approach mentioned in @jonchurch's comment would solve both problems cleanly. With new.target (available in Node.js 6+), the HttpError constructor could distinguish between direct instantiation and subclass calls:

    function HttpError () {
      if (new.target === HttpError) {
        throw new TypeError('cannot construct abstract class')
      }
    }

    This would allow ES6 class extension while preserving the abstract class guard. For the instanceof issue with extending concrete error classes like BadRequest, the constructors would need to not override the prototype — the current setPrototypeOf(err, ClientError.prototype) in the constructor body is what breaks subclass instanceof checks, since it always resets the prototype to the base class.

    Would this be considered for v2 as a non-breaking change (the new.target guard is backwards compatible), or is it bundled with the v3 plan?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions