| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent f0679d9 commit d69d06b
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -151,13 +151,18 @@ function makeSystemErrorWithCode(key) { | |||
| 151 | 151 | }; | |
| 152 | 152 | } | |
| 153 | 153 | ||
| 154 | + let useOriginalName = false; | ||
| 155 | + | ||
| 154 | 156 | function makeNodeErrorWithCode(Base, key) { | |
| 155 | 157 | return class NodeError extends Base { | |
| 156 | 158 | constructor(...args) { | |
| 157 | 159 | super(getMessage(key, args)); | |
| 158 | 160 | } | |
| 159 | 161 | ||
| 160 | 162 | get name() { | |
| 163 | + if (useOriginalName) { | ||
| 164 | + return super.name; | ||
| 165 | + } | ||
| 161 | 166 | return `${super.name} [${key}]`; | |
| 162 | 167 | } | |
| 163 | 168 | ||
@@ -439,7 +444,12 @@ module.exports = { | |||
| 439 | 444 | getMessage, | |
| 440 | 445 | SystemError, | |
| 441 | 446 | codes, | |
| 442 | - E // This is exported only to facilitate testing. | ||
| 447 | + // This is exported only to facilitate testing. | ||
| 448 | + E, | ||
| 449 | + // This allows us to tell the type of the errors without using | ||
| 450 | + // instanceof, which is necessary in WPT harness. | ||
| 451 | + get useOriginalName() { return useOriginalName; }, | ||
| 452 | + set useOriginalName(value) { useOriginalName = value; } | ||
| 443 | 453 | }; | |
| 444 | 454 | ||
| 445 | 455 | // To declare an error message, use the E(sym, val, def) function above. The sym | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,35 @@ | |||
| 1 | + // Flags: --expose-internals | ||
| 2 | + | ||
| 3 | + 'use strict'; | ||
| 4 | + | ||
| 5 | + // This tests `internal/errors.useOriginalName` | ||
| 6 | + // This testing feature is needed to allows us to assert the types of | ||
| 7 | + // errors without using instanceof, which is necessary in WPT harness. | ||
| 8 | + // Refs: https://github.com/nodejs/node/pull/22556 | ||
| 9 | + | ||
| 10 | + require('../common'); | ||
| 11 | + const assert = require('assert'); | ||
| 12 | + const errors = require('internal/errors'); | ||
| 13 | + | ||
| 14 | + | ||
| 15 | + errors.E('TEST_ERROR_1', 'Error for testing purposes: %s', | ||
| 16 | + Error); | ||
| 17 | + { | ||
| 18 | + const err = new errors.codes.TEST_ERROR_1('test'); | ||
| 19 | + assert(err instanceof Error); | ||
| 20 | + assert.strictEqual(err.name, 'Error [TEST_ERROR_1]'); | ||
| 21 | + } | ||
| 22 | + | ||
| 23 | + { | ||
| 24 | + errors.useOriginalName = true; | ||
| 25 | + const err = new errors.codes.TEST_ERROR_1('test'); | ||
| 26 | + assert(err instanceof Error); | ||
| 27 | + assert.strictEqual(err.name, 'Error'); | ||
| 28 | + } | ||
| 29 | + | ||
| 30 | + { | ||
| 31 | + errors.useOriginalName = false; | ||
| 32 | + const err = new errors.codes.TEST_ERROR_1('test'); | ||
| 33 | + assert(err instanceof Error); | ||
| 34 | + assert.strictEqual(err.name, 'Error [TEST_ERROR_1]'); | ||
| 35 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments