From dd1887369505cd172bf20ddd094bec951acdcbc4 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Tue, 28 Aug 2018 03:57:23 +0800 Subject: [PATCH 1/2] errors: add useOriginalName to internal/errors This allows us to tell the type of the errors without using instanceof, which is necessary in WPT harness. --- lib/internal/errors.js | 10 ++++++- .../test-internal-error-original-names.js | 30 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-internal-error-original-names.js diff --git a/lib/internal/errors.js b/lib/internal/errors.js index 6d6bfb7d4db689..998e394dd2051d 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -151,6 +151,8 @@ function makeSystemErrorWithCode(key) { }; } +let useOriginalName = false; + function makeNodeErrorWithCode(Base, key) { return class NodeError extends Base { constructor(...args) { @@ -158,6 +160,9 @@ function makeNodeErrorWithCode(Base, key) { } get name() { + if (useOriginalName) { + return super.name; + } return `${super.name} [${key}]`; } @@ -437,7 +442,10 @@ module.exports = { getMessage, SystemError, codes, - E // This is exported only to facilitate testing. + // These are exported only to facilitate testing. + E, + get useOriginalName() { return useOriginalName; }, + set useOriginalName(value) { useOriginalName = value; } }; // To declare an error message, use the E(sym, val, def) function above. The sym diff --git a/test/parallel/test-internal-error-original-names.js b/test/parallel/test-internal-error-original-names.js new file mode 100644 index 00000000000000..70ea3e9d402469 --- /dev/null +++ b/test/parallel/test-internal-error-original-names.js @@ -0,0 +1,30 @@ +// Flags: --expose-internals + +'use strict'; + +require('../common'); +const assert = require('assert'); +const errors = require('internal/errors'); + + +errors.E('TEST_ERROR_1', 'Error for testing purposes: %s', + Error); +{ + const err = new errors.codes.TEST_ERROR_1('test'); + assert(err instanceof Error); + assert.strictEqual(err.name, 'Error [TEST_ERROR_1]'); +} + +{ + errors.useOriginalName = true; + const err = new errors.codes.TEST_ERROR_1('test'); + assert(err instanceof Error); + assert.strictEqual(err.name, 'Error'); +} + +{ + errors.useOriginalName = false; + const err = new errors.codes.TEST_ERROR_1('test'); + assert(err instanceof Error); + assert.strictEqual(err.name, 'Error [TEST_ERROR_1]'); +} From 2742d395857d3eed33b643d8830d99703ec96a20 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Sat, 15 Sep 2018 13:29:31 +0800 Subject: [PATCH 2/2] fixup! errors: add useOriginalName to internal/errors --- lib/internal/errors.js | 4 +++- test/parallel/test-internal-error-original-names.js | 5 +++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/internal/errors.js b/lib/internal/errors.js index 998e394dd2051d..90bc54927f3de0 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -442,8 +442,10 @@ module.exports = { getMessage, SystemError, codes, - // These are exported only to facilitate testing. + // This is exported only to facilitate testing. E, + // This allows us to tell the type of the errors without using + // instanceof, which is necessary in WPT harness. get useOriginalName() { return useOriginalName; }, set useOriginalName(value) { useOriginalName = value; } }; diff --git a/test/parallel/test-internal-error-original-names.js b/test/parallel/test-internal-error-original-names.js index 70ea3e9d402469..195e39b199cd4c 100644 --- a/test/parallel/test-internal-error-original-names.js +++ b/test/parallel/test-internal-error-original-names.js @@ -2,6 +2,11 @@ 'use strict'; +// This tests `internal/errors.useOriginalName` +// This testing feature is needed to allows us to assert the types of +// errors without using instanceof, which is necessary in WPT harness. +// Refs: https://github.com/nodejs/node/pull/22556 + require('../common'); const assert = require('assert'); const errors = require('internal/errors');