1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
// Flags: --expose-internals
'use strict';
require('../common');
const {
hijackStdout,
restoreStdout,
} = require('../common/hijackstdio');
const assert = require('assert');
const errors = require('internal/errors');
// Turn off ANSI color formatting for this test file.
const { inspect } = require('util');
inspect.defaultOptions.colors = false;
errors.E('TEST_ERROR_1', 'Error for testing purposes: %s',
Error, TypeError, RangeError);
errors.E('TEST_ERROR_2', (a, b) => `${a} ${b}`, Error);
{
const err = new errors.codes.TEST_ERROR_1('test');
assert(err instanceof Error);
assert.strictEqual(err.name, 'Error');
assert.strictEqual(err.message, 'Error for testing purposes: test');
assert.strictEqual(err.code, 'TEST_ERROR_1');
}
{
const err = new errors.codes.TEST_ERROR_1.TypeError('test');
assert(err instanceof TypeError);
assert.strictEqual(err.name, 'TypeError');
assert.strictEqual(err.message, 'Error for testing purposes: test');
assert.strictEqual(err.code, 'TEST_ERROR_1');
}
{
const err = new errors.codes.TEST_ERROR_1.RangeError('test');
assert(err instanceof RangeError);
assert.strictEqual(err.name, 'RangeError');
assert.strictEqual(err.message, 'Error for testing purposes: test');
assert.strictEqual(err.code, 'TEST_ERROR_1');
}
{
const err = new errors.codes.TEST_ERROR_2('abc', 'xyz');
assert(err instanceof Error);
assert.strictEqual(err.name, 'Error');
assert.strictEqual(err.message, 'abc xyz');
assert.strictEqual(err.code, 'TEST_ERROR_2');
}
{
assert.throws(
() => new errors.codes.TEST_ERROR_1(),
{
message: /^Code: TEST_ERROR_1; The provided arguments length \(0\) does not match the required ones \(1\)\./,
name: 'Error',
code: 'ERR_INTERNAL_ASSERTION'
}
);
}
// Tests for common.expectsError
assert.throws(() => {
throw new errors.codes.TEST_ERROR_1.TypeError('a');
}, { code: 'TEST_ERROR_1' });
assert.throws(() => {
throw new errors.codes.TEST_ERROR_1.TypeError('a');
}, { code: 'TEST_ERROR_1',
name: 'TypeError',
message: /^Error for testing/ });
assert.throws(() => {
throw new errors.codes.TEST_ERROR_1.TypeError('a');
}, { code: 'TEST_ERROR_1', name: 'TypeError' });
assert.throws(() => {
throw new errors.codes.TEST_ERROR_1.TypeError('a');
}, {
code: 'TEST_ERROR_1',
name: 'TypeError',
message: 'Error for testing purposes: a'
});
// Test that `code` property is mutable and that changing it does not change the
// name.
{
const myError = new errors.codes.TEST_ERROR_1('foo');
assert.strictEqual(myError.code, 'TEST_ERROR_1');
assert.strictEqual(myError.hasOwnProperty('code'), true);
assert.strictEqual(myError.hasOwnProperty('name'), false);
assert.deepStrictEqual(Object.keys(myError), ['code']);
const initialName = myError.name;
myError.code = 'FHQWHGADS';
assert.strictEqual(myError.code, 'FHQWHGADS');
assert.strictEqual(myError.name, initialName);
assert.deepStrictEqual(Object.keys(myError), ['code']);
assert.ok(!myError.name.includes('TEST_ERROR_1'));
assert.ok(!myError.name.includes('FHQWHGADS'));
}
// 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.
{
const myError = new errors.codes.TEST_ERROR_1('foo');
assert.deepStrictEqual(Object.keys(myError), ['code']);
const initialToString = myError.toString();
myError.name = 'Fhqwhgads';
assert.deepStrictEqual(Object.keys(myError), ['code', '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 = '';
hijackStdout((data) => { initialConsoleLog += data; });
const myError = new errors.codes.TEST_ERROR_1('foo');
assert.deepStrictEqual(Object.keys(myError), ['code']);
const initialToString = myError.toString();
console.log(myError);
assert.notStrictEqual(initialConsoleLog, '');
restoreStdout();
let subsequentConsoleLog = '';
hijackStdout((data) => { subsequentConsoleLog += data; });
myError.message = 'Fhqwhgads';
assert.deepStrictEqual(Object.keys(myError), ['code']);
assert.notStrictEqual(myError.toString(), initialToString);
console.log(myError);
assert.strictEqual(subsequentConsoleLog, initialConsoleLog);
restoreStdout();
}
|