Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove class syntax for better babelification (2) #345

Merged
merged 1 commit into from
Feb 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 25 additions & 31 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down