|
1 | 1 | // Flags: --expose-internals
|
2 | 2 | 'use strict';
|
3 |
| - |
4 | 3 | const common = require('../common');
|
5 |
| -const errors = require('internal/errors'); |
| 4 | + |
6 | 5 | const assert = require('assert');
|
| 6 | +const errors = require('internal/errors'); |
7 | 7 |
|
8 | 8 | function invalidKey(key) {
|
9 | 9 | return new RegExp(`^An invalid error message key was used: ${key}\\.$`);
|
@@ -301,3 +301,44 @@ assert.strictEqual(
|
301 | 301 | 'The value "bar" is invalid for argument "foo"'
|
302 | 302 | );
|
303 | 303 | }
|
| 304 | + |
| 305 | +// Test that `code` property is mutable and that changing it does not change the |
| 306 | +// name. |
| 307 | +{ |
| 308 | + const myError = new errors.Error('ERR_TLS_HANDSHAKE_TIMEOUT'); |
| 309 | + assert.strictEqual(myError.code, 'ERR_TLS_HANDSHAKE_TIMEOUT'); |
| 310 | + const initialName = myError.name; |
| 311 | + myError.code = 'FHQWHGADS'; |
| 312 | + assert.strictEqual(myError.code, 'FHQWHGADS'); |
| 313 | + assert.strictEqual(myError.name, initialName); |
| 314 | + assert.ok(myError.name.includes('ERR_TLS_HANDSHAKE_TIMEOUT')); |
| 315 | + assert.ok(!myError.name.includes('FHQWHGADS')); |
| 316 | +} |
| 317 | + |
| 318 | +// Test that `name` and `message` are mutable and that changing them alters |
| 319 | +// `toString()` but not `console.log()` results, which is the behavior of |
| 320 | +// `Error` objects in the browser. |
| 321 | +{ |
| 322 | + function test(prop) { |
| 323 | + let initialConsoleLog = ''; |
| 324 | + common.hijackStdout((data) => { initialConsoleLog += data; }); |
| 325 | + const myError = new errors.Error('ERR_TLS_HANDSHAKE_TIMEOUT'); |
| 326 | + const initialToString = myError.toString(); |
| 327 | + console.log(myError); |
| 328 | + assert.notStrictEqual(initialConsoleLog, ''); |
| 329 | + |
| 330 | + common.restoreStdout(); |
| 331 | + |
| 332 | + let subsequentConsoleLog = ''; |
| 333 | + common.hijackStdout((data) => { subsequentConsoleLog += data; }); |
| 334 | + myError[prop] = 'Fhqwhgads'; |
| 335 | + assert.notStrictEqual(myError.toString(), initialToString); |
| 336 | + console.log(myError); |
| 337 | + assert.strictEqual(subsequentConsoleLog, initialConsoleLog); |
| 338 | + |
| 339 | + common.restoreStdout(); |
| 340 | + } |
| 341 | + |
| 342 | + test('name'); |
| 343 | + test('message'); |
| 344 | +} |
0 commit comments