Skip to content

Commit

Permalink
Align Node error objects with Node 12.2.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalton committed May 9, 2019
1 parent 25e6299 commit a0a21ac
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 35 deletions.
27 changes: 15 additions & 12 deletions src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,23 @@ function init() {
return class NodeError extends Super {
constructor(...args) {
const template = templateMap.get(code)

super(template(...args))

if (code === "MODULE_NOT_FOUND") {
this.code = code
this.name = super.name
}
const name = toString(get(this, "name"))

// Add the error code to the name to include it in the stack trace.
Reflect.defineProperty(this, "name", {
configurable: true,
value: name + " [" + code + "]",
writable: true
})

// Access the stack to generate the error message including the error
// code from the name.
get(this, "stack")
// Reset the name to the actual name.
Reflect.deleteProperty(this, "name")
}

get code() {
Expand All @@ -159,14 +170,6 @@ function init() {
set code(value) {
setProperty(this, "code", value)
}

get name() {
return super.name + " [" + code + "]"
}

set name(value) {
setProperty(this, "name", value)
}
}
}

Expand Down
25 changes: 2 additions & 23 deletions test/misc-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,30 +49,9 @@ const defNs = createNamespace({
function checkError(error, code) {
const { message } = error

checkErrorProps(error, code, message)
checkErrorCustomProps(error, code, message)
checkErrorProps(error, code, message)
}

function checkErrorCustomProps(error, code, message) {
error.code = "ERR_CUSTOM"

assert.strictEqual(error.code, "ERR_CUSTOM")
assert.strictEqual(error.toString(), "Error [" + code + "]: " + message)

error.name = "Custom"

assert.strictEqual(error.name, "Custom")
assert.strictEqual(error.toString(), "Custom: " + message)

Reflect.deleteProperty(error, "code")
Reflect.deleteProperty(error, "name")
}

function checkErrorProps(error, code, message) {
assert.strictEqual(error.code, code)
assert.strictEqual(error.name, "Error [" + code + "]")
assert.strictEqual(error.toString(), "Error [" + code + "]: " + message)
assert.strictEqual(error.message, message)
assert.strictEqual(error.name, "Error")
assert.deepStrictEqual(Object.keys(error), [])
assert.deepStrictEqual(Object.getOwnPropertySymbols(error), [])
}
Expand Down

0 comments on commit a0a21ac

Please sign in to comment.