2016-10-24 13:09:34 -07:00
|
|
|
// Flags: --expose-internals
|
|
|
|
'use strict';
|
|
|
|
const common = require('../common');
|
2018-08-22 09:17:19 -07:00
|
|
|
const {
|
|
|
|
hijackStdout,
|
|
|
|
restoreStdout,
|
|
|
|
} = require('../common/hijackstdio');
|
2018-08-17 16:33:45 +08:00
|
|
|
|
2016-10-24 13:09:34 -07:00
|
|
|
const assert = require('assert');
|
2017-09-29 17:06:28 -07:00
|
|
|
const errors = require('internal/errors');
|
2016-10-24 13:09:34 -07:00
|
|
|
|
2018-11-06 16:04:08 -05:00
|
|
|
// Turn off ANSI color formatting for this test file.
|
|
|
|
const { inspect } = require('util');
|
|
|
|
inspect.defaultOptions.colors = false;
|
|
|
|
|
2018-03-06 14:17:37 +01:00
|
|
|
errors.E('TEST_ERROR_1', 'Error for testing purposes: %s',
|
|
|
|
Error, TypeError, RangeError);
|
|
|
|
errors.E('TEST_ERROR_2', (a, b) => `${a} ${b}`, Error);
|
2016-10-24 13:09:34 -07:00
|
|
|
|
2017-10-01 19:54:37 -07:00
|
|
|
{
|
2018-03-06 14:17:37 +01:00
|
|
|
const err = new errors.codes.TEST_ERROR_1('test');
|
2017-10-01 19:54:37 -07:00
|
|
|
assert(err instanceof Error);
|
2019-03-16 12:09:14 +01:00
|
|
|
assert.strictEqual(err.name, 'Error');
|
2017-10-01 19:54:37 -07:00
|
|
|
assert.strictEqual(err.message, 'Error for testing purposes: test');
|
|
|
|
assert.strictEqual(err.code, 'TEST_ERROR_1');
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2018-03-06 14:17:37 +01:00
|
|
|
const err = new errors.codes.TEST_ERROR_1.TypeError('test');
|
2017-10-01 19:54:37 -07:00
|
|
|
assert(err instanceof TypeError);
|
2019-03-16 12:09:14 +01:00
|
|
|
assert.strictEqual(err.name, 'TypeError');
|
2017-10-01 19:54:37 -07:00
|
|
|
assert.strictEqual(err.message, 'Error for testing purposes: test');
|
|
|
|
assert.strictEqual(err.code, 'TEST_ERROR_1');
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2018-03-06 14:17:37 +01:00
|
|
|
const err = new errors.codes.TEST_ERROR_1.RangeError('test');
|
2017-10-01 19:54:37 -07:00
|
|
|
assert(err instanceof RangeError);
|
2019-03-16 12:09:14 +01:00
|
|
|
assert.strictEqual(err.name, 'RangeError');
|
2017-10-01 19:54:37 -07:00
|
|
|
assert.strictEqual(err.message, 'Error for testing purposes: test');
|
|
|
|
assert.strictEqual(err.code, 'TEST_ERROR_1');
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2018-03-06 14:17:37 +01:00
|
|
|
const err = new errors.codes.TEST_ERROR_2('abc', 'xyz');
|
2017-10-01 19:54:37 -07:00
|
|
|
assert(err instanceof Error);
|
2019-03-16 12:09:14 +01:00
|
|
|
assert.strictEqual(err.name, 'Error');
|
2017-10-01 19:54:37 -07:00
|
|
|
assert.strictEqual(err.message, 'abc xyz');
|
|
|
|
assert.strictEqual(err.code, 'TEST_ERROR_2');
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2019-03-13 22:43:00 +08:00
|
|
|
common.expectsInternalAssertion(
|
2018-04-11 03:10:22 +02:00
|
|
|
() => new errors.codes.TEST_ERROR_1(),
|
2019-03-13 22:43:00 +08:00
|
|
|
'Code: TEST_ERROR_1; The provided arguments ' +
|
|
|
|
'length (0) does not match the required ones (1).'
|
2018-04-11 03:10:22 +02:00
|
|
|
);
|
2017-10-01 19:54:37 -07:00
|
|
|
}
|
2017-03-14 00:11:35 +08:00
|
|
|
|
2016-10-24 13:09:34 -07:00
|
|
|
// Tests for common.expectsError
|
2018-02-09 02:32:04 +01:00
|
|
|
common.expectsError(() => {
|
2018-03-06 14:17:37 +01:00
|
|
|
throw new errors.codes.TEST_ERROR_1.TypeError('a');
|
2018-02-09 02:32:04 +01:00
|
|
|
}, { code: 'TEST_ERROR_1' });
|
|
|
|
common.expectsError(() => {
|
2018-03-06 14:17:37 +01:00
|
|
|
throw new errors.codes.TEST_ERROR_1.TypeError('a');
|
2018-02-09 02:32:04 +01:00
|
|
|
}, { code: 'TEST_ERROR_1',
|
|
|
|
type: TypeError,
|
|
|
|
message: /^Error for testing/ });
|
|
|
|
common.expectsError(() => {
|
2018-03-06 14:17:37 +01:00
|
|
|
throw new errors.codes.TEST_ERROR_1.TypeError('a');
|
2018-02-09 02:32:04 +01:00
|
|
|
}, { code: 'TEST_ERROR_1', type: TypeError });
|
|
|
|
common.expectsError(() => {
|
2018-03-06 14:17:37 +01:00
|
|
|
throw new errors.codes.TEST_ERROR_1.TypeError('a');
|
2018-02-09 02:32:04 +01:00
|
|
|
}, {
|
|
|
|
code: 'TEST_ERROR_1',
|
|
|
|
type: TypeError,
|
|
|
|
message: 'Error for testing purposes: a'
|
2016-10-24 13:09:34 -07:00
|
|
|
});
|
|
|
|
|
2017-09-29 17:06:28 -07:00
|
|
|
// Test that `code` property is mutable and that changing it does not change the
|
|
|
|
// name.
|
|
|
|
{
|
2019-03-18 18:22:54 +01:00
|
|
|
const myError = new errors.codes.TEST_ERROR_1('foo');
|
|
|
|
assert.strictEqual(myError.code, 'TEST_ERROR_1');
|
2017-10-01 16:14:45 -07:00
|
|
|
assert.strictEqual(myError.hasOwnProperty('code'), false);
|
|
|
|
assert.strictEqual(myError.hasOwnProperty('name'), false);
|
|
|
|
assert.deepStrictEqual(Object.keys(myError), []);
|
2017-09-29 17:06:28 -07:00
|
|
|
const initialName = myError.name;
|
|
|
|
myError.code = 'FHQWHGADS';
|
|
|
|
assert.strictEqual(myError.code, 'FHQWHGADS');
|
|
|
|
assert.strictEqual(myError.name, initialName);
|
2017-10-01 16:14:45 -07:00
|
|
|
assert.deepStrictEqual(Object.keys(myError), ['code']);
|
2019-03-16 12:09:14 +01:00
|
|
|
assert.ok(!myError.name.includes('TEST_ERROR_1'));
|
2017-09-29 17:06:28 -07:00
|
|
|
assert.ok(!myError.name.includes('FHQWHGADS'));
|
|
|
|
}
|
|
|
|
|
2017-10-01 16:14:45 -07:00
|
|
|
// Test that `name` is mutable and that changing it alters `toString()` but not
|
|
|
|
// `console.log()` results, which is the behavior of `Error` objects in the
|
|
|
|
// browser. Note that `name` becomes enumerable after being assigned.
|
2017-09-29 17:06:28 -07:00
|
|
|
{
|
2019-03-18 18:22:54 +01:00
|
|
|
const myError = new errors.codes.TEST_ERROR_1('foo');
|
2017-10-01 16:14:45 -07:00
|
|
|
assert.deepStrictEqual(Object.keys(myError), []);
|
|
|
|
const initialToString = myError.toString();
|
|
|
|
|
|
|
|
myError.name = 'Fhqwhgads';
|
|
|
|
assert.deepStrictEqual(Object.keys(myError), ['name']);
|
|
|
|
assert.notStrictEqual(myError.toString(), initialToString);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that `message` is mutable and that changing it alters `toString()` but
|
|
|
|
// not `console.log()` results, which is the behavior of `Error` objects in the
|
|
|
|
// browser. Note that `message` remains non-enumerable after being assigned.
|
|
|
|
{
|
|
|
|
let initialConsoleLog = '';
|
2018-08-22 09:17:19 -07:00
|
|
|
hijackStdout((data) => { initialConsoleLog += data; });
|
2019-03-18 18:22:54 +01:00
|
|
|
const myError = new errors.codes.TEST_ERROR_1('foo');
|
2017-10-01 16:14:45 -07:00
|
|
|
assert.deepStrictEqual(Object.keys(myError), []);
|
|
|
|
const initialToString = myError.toString();
|
|
|
|
console.log(myError);
|
|
|
|
assert.notStrictEqual(initialConsoleLog, '');
|
|
|
|
|
2018-08-22 09:17:19 -07:00
|
|
|
restoreStdout();
|
2017-10-01 16:14:45 -07:00
|
|
|
|
|
|
|
let subsequentConsoleLog = '';
|
2018-08-22 09:17:19 -07:00
|
|
|
hijackStdout((data) => { subsequentConsoleLog += data; });
|
2017-10-01 16:14:45 -07:00
|
|
|
myError.message = 'Fhqwhgads';
|
|
|
|
assert.deepStrictEqual(Object.keys(myError), []);
|
|
|
|
assert.notStrictEqual(myError.toString(), initialToString);
|
|
|
|
console.log(myError);
|
|
|
|
assert.strictEqual(subsequentConsoleLog, initialConsoleLog);
|
|
|
|
|
2018-08-22 09:17:19 -07:00
|
|
|
restoreStdout();
|
2017-09-29 17:06:28 -07:00
|
|
|
}
|