Skip to content
This repository was archived by the owner on Nov 4, 2024. It is now read-only.

Commit a0a21ac

Browse files
committed
Align Node error objects with Node 12.2.0.
1 parent 25e6299 commit a0a21ac

File tree

2 files changed

+17
-35
lines changed

2 files changed

+17
-35
lines changed

src/errors.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,23 @@ function init() {
144144
return class NodeError extends Super {
145145
constructor(...args) {
146146
const template = templateMap.get(code)
147+
147148
super(template(...args))
148149

149-
if (code === "MODULE_NOT_FOUND") {
150-
this.code = code
151-
this.name = super.name
152-
}
150+
const name = toString(get(this, "name"))
151+
152+
// Add the error code to the name to include it in the stack trace.
153+
Reflect.defineProperty(this, "name", {
154+
configurable: true,
155+
value: name + " [" + code + "]",
156+
writable: true
157+
})
158+
159+
// Access the stack to generate the error message including the error
160+
// code from the name.
161+
get(this, "stack")
162+
// Reset the name to the actual name.
163+
Reflect.deleteProperty(this, "name")
153164
}
154165

155166
get code() {
@@ -159,14 +170,6 @@ function init() {
159170
set code(value) {
160171
setProperty(this, "code", value)
161172
}
162-
163-
get name() {
164-
return super.name + " [" + code + "]"
165-
}
166-
167-
set name(value) {
168-
setProperty(this, "name", value)
169-
}
170173
}
171174
}
172175

test/misc-tests.js

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -49,30 +49,9 @@ const defNs = createNamespace({
4949
function checkError(error, code) {
5050
const { message } = error
5151

52-
checkErrorProps(error, code, message)
53-
checkErrorCustomProps(error, code, message)
54-
checkErrorProps(error, code, message)
55-
}
56-
57-
function checkErrorCustomProps(error, code, message) {
58-
error.code = "ERR_CUSTOM"
59-
60-
assert.strictEqual(error.code, "ERR_CUSTOM")
61-
assert.strictEqual(error.toString(), "Error [" + code + "]: " + message)
62-
63-
error.name = "Custom"
64-
65-
assert.strictEqual(error.name, "Custom")
66-
assert.strictEqual(error.toString(), "Custom: " + message)
67-
68-
Reflect.deleteProperty(error, "code")
69-
Reflect.deleteProperty(error, "name")
70-
}
71-
72-
function checkErrorProps(error, code, message) {
7352
assert.strictEqual(error.code, code)
74-
assert.strictEqual(error.name, "Error [" + code + "]")
75-
assert.strictEqual(error.toString(), "Error [" + code + "]: " + message)
53+
assert.strictEqual(error.message, message)
54+
assert.strictEqual(error.name, "Error")
7655
assert.deepStrictEqual(Object.keys(error), [])
7756
assert.deepStrictEqual(Object.getOwnPropertySymbols(error), [])
7857
}

0 commit comments

Comments
 (0)