summaryrefslogtreecommitdiff
path: root/lib/internal/errors.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/internal/errors.js')
-rw-r--r--lib/internal/errors.js65
1 files changed, 34 insertions, 31 deletions
diff --git a/lib/internal/errors.js b/lib/internal/errors.js
index d283fda143..536778db43 100644
--- a/lib/internal/errors.js
+++ b/lib/internal/errors.js
@@ -18,6 +18,8 @@ const codes = {};
const { kMaxLength } = internalBinding('buffer');
const { defineProperty } = Object;
+let useOriginalName = false;
+
// Lazily loaded
let util;
let assert;
@@ -74,19 +76,7 @@ class SystemError extends Error {
value: key,
writable: true
});
- }
-
- get name() {
- return `SystemError [${this[kCode]}]`;
- }
-
- set name(value) {
- defineProperty(this, 'name', {
- configurable: true,
- enumerable: true,
- value,
- writable: true
- });
+ addCodeToName(this, 'SystemError', key);
}
get code() {
@@ -141,6 +131,10 @@ class SystemError extends Error {
this[kInfo].dest = val ?
lazyBuffer().from(val.toString()) : undefined;
}
+
+ toString() {
+ return `${this.name} [${this.code}]: ${this.message}`;
+ }
}
function makeSystemErrorWithCode(key) {
@@ -151,8 +145,6 @@ function makeSystemErrorWithCode(key) {
};
}
-let useOriginalName = false;
-
function makeNodeErrorWithCode(Base, key) {
return class NodeError extends Base {
constructor(...args) {
@@ -164,22 +156,7 @@ function makeNodeErrorWithCode(Base, key) {
writable: true,
configurable: true
});
- }
-
- get name() {
- if (useOriginalName) {
- return super.name;
- }
- return `${super.name} [${key}]`;
- }
-
- set name(value) {
- defineProperty(this, 'name', {
- configurable: true,
- enumerable: true,
- value,
- writable: true
- });
+ addCodeToName(this, super.name, key);
}
get code() {
@@ -194,9 +171,35 @@ function makeNodeErrorWithCode(Base, key) {
writable: true
});
}
+
+ toString() {
+ return `${this.name} [${key}]: ${this.message}`;
+ }
};
}
+function addCodeToName(err, name, code) {
+ if (useOriginalName) {
+ return;
+ }
+ // Add the error code to the name to include it in the stack trace.
+ err.name = `${name} [${code}]`;
+ // Access the stack to generate the error message including the error code
+ // from the name.
+ err.stack;
+ // Reset the name to the actual name.
+ if (name === 'SystemError') {
+ defineProperty(err, 'name', {
+ value: name,
+ enumerable: false,
+ writable: true,
+ configurable: true
+ });
+ } else {
+ delete err.name;
+ }
+}
+
// Utility function for registering the error codes. Only used here. Exported
// *only* to allow for testing.
function E(sym, val, def, ...otherClasses) {