Skip to content

Commit

Permalink
improve perf of error creation (#109)
Browse files Browse the repository at this point in the history
* improve perf of error creation

* more like the original code
  • Loading branch information
Uzlopak authored Jun 23, 2023
1 parent 436b46d commit ffe0aab
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
'use strict'

const { inherits, format } = require('util')
const { format } = require('util')

function toString () {
return `${this.name} [${this.code}]: ${this.message}`
}

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

FastifyError.prototype[Symbol.toStringTag] = 'Error'
FastifyError.prototype = Object.create(Base.prototype, {
constructor: {
value: FastifyError,
enumerable: false,
writable: true,
configurable: true
}
})

FastifyError.prototype.toString = function () {
return `${this.name} [${this.code}]: ${this.message}`
}
FastifyError.prototype[Symbol.toStringTag] = 'Error'

inherits(FastifyError, Base)
FastifyError.prototype.toString = toString

return FastifyError
}
Expand Down

0 comments on commit ffe0aab

Please sign in to comment.