You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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:
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?
Activity
jonchurch commentedon Mar 8, 2024
moving this comment and repro from another issue #101 :
#101 (comment)
jonchurch commentedon Mar 9, 2024
There's two different concepts in this issue.
Extending
HttpErroris not possible currently because of the throw to prevent instantiating the abstract class.Extending error classes like
NotFounddo not wire up the prototype properly to be able to checkinsanceofon it.mahmoodhamdi commentedon Mar 29, 2026
I ran into this limitation when trying to create custom error subclasses in my Express app. The current workaround is verbose:
The
new.targetapproach mentioned in @jonchurch's comment would solve both problems cleanly. Withnew.target(available in Node.js 6+), the HttpError constructor could distinguish between direct instantiation and subclass calls:This would allow ES6 class extension while preserving the abstract class guard. For the
instanceofissue with extending concrete error classes likeBadRequest, the constructors would need to not override the prototype — the currentsetPrototypeOf(err, ClientError.prototype)in the constructor body is what breaks subclassinstanceofchecks, since it always resets the prototype to the base class.Would this be considered for v2 as a non-breaking change (the
new.targetguard is backwards compatible), or is it bundled with the v3 plan?