Skip to content

Commit ffe0aab

Browse files
authored
improve perf of error creation (#109)
* improve perf of error creation * more like the original code
1 parent 436b46d commit ffe0aab

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

index.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
'use strict'
22

3-
const { inherits, format } = require('util')
3+
const { format } = require('util')
4+
5+
function toString () {
6+
return `${this.name} [${this.code}]: ${this.message}`
7+
}
48

59
function createError (code, message, statusCode = 500, Base = Error) {
610
if (!code) throw new Error('Fastify error code must not be empty')
@@ -22,13 +26,18 @@ function createError (code, message, statusCode = 500, Base = Error) {
2226
Error.stackTraceLimit !== 0 && Error.captureStackTrace(this, FastifyError)
2327
}
2428

25-
FastifyError.prototype[Symbol.toStringTag] = 'Error'
29+
FastifyError.prototype = Object.create(Base.prototype, {
30+
constructor: {
31+
value: FastifyError,
32+
enumerable: false,
33+
writable: true,
34+
configurable: true
35+
}
36+
})
2637

27-
FastifyError.prototype.toString = function () {
28-
return `${this.name} [${this.code}]: ${this.message}`
29-
}
38+
FastifyError.prototype[Symbol.toStringTag] = 'Error'
3039

31-
inherits(FastifyError, Base)
40+
FastifyError.prototype.toString = toString
3241

3342
return FastifyError
3443
}

0 commit comments

Comments
 (0)