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
|
// Flags: --expose-internals
'use strict';
const common = require('../common');
const errors = require('internal/errors');
const assert = require('assert');
errors.E('TEST_ERROR_1', 'Error for testing purposes: %s');
errors.E('TEST_ERROR_2', (a, b) => `${a} ${b}`);
const err1 = new errors.Error('TEST_ERROR_1', 'test');
const err2 = new errors.TypeError('TEST_ERROR_1', 'test');
const err3 = new errors.RangeError('TEST_ERROR_1', 'test');
const err4 = new errors.Error('TEST_ERROR_2', 'abc', 'xyz');
assert(err1 instanceof Error);
assert.strictEqual(err1.name, 'Error[TEST_ERROR_1]');
assert.strictEqual(err1.message, 'Error for testing purposes: test');
assert.strictEqual(err1.code, 'TEST_ERROR_1');
assert(err2 instanceof TypeError);
assert.strictEqual(err2.name, 'TypeError[TEST_ERROR_1]');
assert.strictEqual(err2.message, 'Error for testing purposes: test');
assert.strictEqual(err2.code, 'TEST_ERROR_1');
assert(err3 instanceof RangeError);
assert.strictEqual(err3.name, 'RangeError[TEST_ERROR_1]');
assert.strictEqual(err3.message, 'Error for testing purposes: test');
assert.strictEqual(err3.code, 'TEST_ERROR_1');
assert(err4 instanceof Error);
assert.strictEqual(err4.name, 'Error[TEST_ERROR_2]');
assert.strictEqual(err4.message, 'abc xyz');
assert.strictEqual(err4.code, 'TEST_ERROR_2');
assert.throws(
() => new errors.Error('TEST_FOO_KEY'),
/^AssertionError: An invalid error message key was used: TEST_FOO_KEY.$/);
// Calling it twice yields same result (using the key does not create it)
assert.throws(
() => new errors.Error('TEST_FOO_KEY'),
/^AssertionError: An invalid error message key was used: TEST_FOO_KEY.$/);
assert.throws(
() => new errors.Error(1),
/^AssertionError: 'number' === 'string'$/);
assert.throws(
() => new errors.Error({}),
/^AssertionError: 'object' === 'string'$/);
assert.throws(
() => new errors.Error([]),
/^AssertionError: 'object' === 'string'$/);
assert.throws(
() => new errors.Error(true),
/^AssertionError: 'boolean' === 'string'$/);
assert.throws(
() => new errors.TypeError(1),
/^AssertionError: 'number' === 'string'$/);
assert.throws(
() => new errors.TypeError({}),
/^AssertionError: 'object' === 'string'$/);
assert.throws(
() => new errors.TypeError([]),
/^AssertionError: 'object' === 'string'$/);
assert.throws(
() => new errors.TypeError(true),
/^AssertionError: 'boolean' === 'string'$/);
assert.throws(
() => new errors.RangeError(1),
/^AssertionError: 'number' === 'string'$/);
assert.throws(
() => new errors.RangeError({}),
/^AssertionError: 'object' === 'string'$/);
assert.throws(
() => new errors.RangeError([]),
/^AssertionError: 'object' === 'string'$/);
assert.throws(
() => new errors.RangeError(true),
/^AssertionError: 'boolean' === 'string'$/);
// Tests for common.expectsError
assert.doesNotThrow(() => {
assert.throws(() => {
throw new errors.TypeError('TEST_ERROR_1', 'a');
}, common.expectsError('TEST_ERROR_1'));
});
assert.doesNotThrow(() => {
assert.throws(() => {
throw new errors.TypeError('TEST_ERROR_1', 'a');
}, common.expectsError('TEST_ERROR_1', TypeError, /^Error for testing/));
});
assert.doesNotThrow(() => {
assert.throws(() => {
throw new errors.TypeError('TEST_ERROR_1', 'a');
}, common.expectsError('TEST_ERROR_1', TypeError));
});
assert.doesNotThrow(() => {
assert.throws(() => {
throw new errors.TypeError('TEST_ERROR_1', 'a');
}, common.expectsError('TEST_ERROR_1', Error));
});
assert.throws(() => {
assert.throws(() => {
throw new errors.TypeError('TEST_ERROR_1', 'a');
}, common.expectsError('TEST_ERROR_1', RangeError));
}, /^AssertionError: .+ is not the expected type \S/);
assert.throws(() => {
assert.throws(() => {
throw new errors.TypeError('TEST_ERROR_1', 'a');
}, common.expectsError('TEST_ERROR_1', TypeError, /^Error for testing 2/));
}, /AssertionError: .+ does not match \S/);
|