From d80ce56c77dcca511dfcba4975913d3d5e207af8 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Tue, 23 Jan 2024 15:40:23 -0500 Subject: [PATCH] Remove class syntax for better babelification --- index.js | 56 +++++++++++++++++++++++++------------------------------- 1 file changed, 25 insertions(+), 31 deletions(-) diff --git a/index.js b/index.js index dad9152..5a11eb4 100644 --- a/index.js +++ b/index.js @@ -1826,42 +1826,36 @@ Buffer.prototype.fill = function fill (val, start, end, encoding) { // Simplified versions from Node, changed for Buffer-only usage const errors = {} function E (sym, getMessage, Base) { - errors[sym] = class NodeError extends Base { - constructor () { - super() - - Object.defineProperty(this, 'message', { - value: getMessage.apply(this, arguments), - writable: true, - configurable: true - }) - - // Add the error code to the name to include it in the stack trace. - this.name = `${this.name} [${sym}]` - // Access the stack to generate the error message including the error code - // from the name. - this.stack // eslint-disable-line no-unused-expressions - // Reset the name to the actual name. - delete this.name + function NodeError () { + const err = new Base(getMessage.apply(null, arguments)) + + Object.setPrototypeOf(err, NodeError.prototype) + + // Node.js `err.code` properties are own/enumerable properties. + err.code = sym + // Add the error code to the name to include it in the stack trace. + err.name = `${err.name} [${sym}]` + // Remove NodeError from the stack trace. + if (Error.captureStackTrace) { + Error.captureStackTrace(err, NodeError) } + // Access the stack to generate the error message including the error code + // from the name. + err.stack // eslint-disable-line no-unused-expressions + // Reset the name to the actual name. + delete err.name - get code () { - return sym - } + return err + } - set code (value) { - Object.defineProperty(this, 'code', { - configurable: true, - enumerable: true, - value, - writable: true - }) - } + Object.setPrototypeOf(NodeError.prototype, Base.prototype) + Object.setPrototypeOf(NodeError, Base) - toString () { - return `${this.name} [${sym}]: ${this.message}` - } + NodeError.prototype.toString = function toString () { + return `${this.name} [${sym}]: ${this.message}` } + + errors[sym] = NodeError } E('ERR_BUFFER_OUT_OF_BOUNDS',