@@ -111,7 +111,9 @@ function createError () {
111
111
112
112
function createHttpErrorConstructor ( ) {
113
113
function HttpError ( ) {
114
- throw new TypeError ( 'cannot construct abstract class' )
114
+ if ( new . target === HttpError ) {
115
+ throw new TypeError ( 'cannot construct abstract class' )
116
+ }
115
117
}
116
118
117
119
inherits ( HttpError , Error )
@@ -130,13 +132,15 @@ function createClientErrorConstructor (HttpError, name, code) {
130
132
function ClientError ( message ) {
131
133
// create the error object
132
134
var msg = message != null ? message : statuses . message [ code ]
133
- var err = new Error ( msg )
135
+ var err = Reflect . construct ( Error , [ msg ] , new . target || ClientError )
134
136
135
137
// capture a stack trace to the construction point
136
138
Error . captureStackTrace ( err , ClientError )
137
139
138
- // adjust the [[Prototype]]
139
- setPrototypeOf ( err , ClientError . prototype )
140
+ // adjust the [[Prototype]] if new.target is not ClientError
141
+ if ( new . target && new . target !== ClientError ) {
142
+ setPrototypeOf ( err , new . target . prototype )
143
+ }
140
144
141
145
// redefine the error message
142
146
Object . defineProperty ( err , 'message' , {
@@ -199,13 +203,15 @@ function createServerErrorConstructor (HttpError, name, code) {
199
203
function ServerError ( message ) {
200
204
// create the error object
201
205
var msg = message != null ? message : statuses . message [ code ]
202
- var err = new Error ( msg )
206
+ var err = Reflect . construct ( Error , [ msg ] , new . target || ServerError )
203
207
204
208
// capture a stack trace to the construction point
205
209
Error . captureStackTrace ( err , ServerError )
206
210
207
- // adjust the [[Prototype]]
208
- setPrototypeOf ( err , ServerError . prototype )
211
+ // adjust the [[Prototype]] if new.target is not ClientError
212
+ if ( new . target && new . target !== ServerError ) {
213
+ setPrototypeOf ( err , new . target . prototype )
214
+ }
209
215
210
216
// redefine the error message
211
217
Object . defineProperty ( err , 'message' , {
0 commit comments