Skip to content

Commit

Permalink
Remove class syntax for better babelification (#345)
Browse files Browse the repository at this point in the history
  • Loading branch information
chjj authored Feb 5, 2024
1 parent d0092f7 commit 7291685
Showing 1 changed file with 25 additions and 31 deletions.
56 changes: 25 additions & 31 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1836,42 +1836,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',
Expand Down

0 comments on commit 7291685

Please sign in to comment.