|
| 1 | +// Flags: --expose-internals |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +const common = require('../common'); |
| 5 | +const errors = require('internal/errors'); |
| 6 | +const assert = require('assert'); |
| 7 | + |
| 8 | +errors.E('TEST_ERROR_1', 'Error for testing purposes: %s'); |
| 9 | +errors.E('TEST_ERROR_2', (a, b) => `${a} ${b}`); |
| 10 | + |
| 11 | +const err1 = new errors.Error('TEST_ERROR_1', 'test'); |
| 12 | +const err2 = new errors.TypeError('TEST_ERROR_1', 'test'); |
| 13 | +const err3 = new errors.RangeError('TEST_ERROR_1', 'test'); |
| 14 | +const err4 = new errors.Error('TEST_ERROR_2', 'abc', 'xyz'); |
| 15 | + |
| 16 | +assert(err1 instanceof Error); |
| 17 | +assert.strictEqual(err1.name, 'Error[TEST_ERROR_1]'); |
| 18 | +assert.strictEqual(err1.message, 'Error for testing purposes: test'); |
| 19 | +assert.strictEqual(err1.code, 'TEST_ERROR_1'); |
| 20 | + |
| 21 | +assert(err2 instanceof TypeError); |
| 22 | +assert.strictEqual(err2.name, 'TypeError[TEST_ERROR_1]'); |
| 23 | +assert.strictEqual(err2.message, 'Error for testing purposes: test'); |
| 24 | +assert.strictEqual(err2.code, 'TEST_ERROR_1'); |
| 25 | + |
| 26 | +assert(err3 instanceof RangeError); |
| 27 | +assert.strictEqual(err3.name, 'RangeError[TEST_ERROR_1]'); |
| 28 | +assert.strictEqual(err3.message, 'Error for testing purposes: test'); |
| 29 | +assert.strictEqual(err3.code, 'TEST_ERROR_1'); |
| 30 | + |
| 31 | +assert(err4 instanceof Error); |
| 32 | +assert.strictEqual(err4.name, 'Error[TEST_ERROR_2]'); |
| 33 | +assert.strictEqual(err4.message, 'abc xyz'); |
| 34 | +assert.strictEqual(err4.code, 'TEST_ERROR_2'); |
| 35 | + |
| 36 | +assert.throws( |
| 37 | + () => new errors.Error('TEST_FOO_KEY'), |
| 38 | + /^AssertionError: An invalid error message key was used: TEST_FOO_KEY.$/); |
| 39 | +// Calling it twice yields same result (using the key does not create it) |
| 40 | +assert.throws( |
| 41 | + () => new errors.Error('TEST_FOO_KEY'), |
| 42 | + /^AssertionError: An invalid error message key was used: TEST_FOO_KEY.$/); |
| 43 | +assert.throws( |
| 44 | + () => new errors.Error(1), |
| 45 | + /^AssertionError: 'number' === 'string'$/); |
| 46 | +assert.throws( |
| 47 | + () => new errors.Error({}), |
| 48 | + /^AssertionError: 'object' === 'string'$/); |
| 49 | +assert.throws( |
| 50 | + () => new errors.Error([]), |
| 51 | + /^AssertionError: 'object' === 'string'$/); |
| 52 | +assert.throws( |
| 53 | + () => new errors.Error(true), |
| 54 | + /^AssertionError: 'boolean' === 'string'$/); |
| 55 | +assert.throws( |
| 56 | + () => new errors.TypeError(1), |
| 57 | + /^AssertionError: 'number' === 'string'$/); |
| 58 | +assert.throws( |
| 59 | + () => new errors.TypeError({}), |
| 60 | + /^AssertionError: 'object' === 'string'$/); |
| 61 | +assert.throws( |
| 62 | + () => new errors.TypeError([]), |
| 63 | + /^AssertionError: 'object' === 'string'$/); |
| 64 | +assert.throws( |
| 65 | + () => new errors.TypeError(true), |
| 66 | + /^AssertionError: 'boolean' === 'string'$/); |
| 67 | +assert.throws( |
| 68 | + () => new errors.RangeError(1), |
| 69 | + /^AssertionError: 'number' === 'string'$/); |
| 70 | +assert.throws( |
| 71 | + () => new errors.RangeError({}), |
| 72 | + /^AssertionError: 'object' === 'string'$/); |
| 73 | +assert.throws( |
| 74 | + () => new errors.RangeError([]), |
| 75 | + /^AssertionError: 'object' === 'string'$/); |
| 76 | +assert.throws( |
| 77 | + () => new errors.RangeError(true), |
| 78 | + /^AssertionError: 'boolean' === 'string'$/); |
| 79 | + |
| 80 | + |
| 81 | +// Tests for common.expectsError |
| 82 | +assert.doesNotThrow(() => { |
| 83 | + assert.throws(() => { |
| 84 | + throw new errors.TypeError('TEST_ERROR_1', 'a'); |
| 85 | + }, common.expectsError('TEST_ERROR_1')); |
| 86 | +}); |
| 87 | + |
| 88 | +assert.doesNotThrow(() => { |
| 89 | + assert.throws(() => { |
| 90 | + throw new errors.TypeError('TEST_ERROR_1', 'a'); |
| 91 | + }, common.expectsError('TEST_ERROR_1', TypeError, /^Error for testing/)); |
| 92 | +}); |
| 93 | + |
| 94 | +assert.doesNotThrow(() => { |
| 95 | + assert.throws(() => { |
| 96 | + throw new errors.TypeError('TEST_ERROR_1', 'a'); |
| 97 | + }, common.expectsError('TEST_ERROR_1', TypeError)); |
| 98 | +}); |
| 99 | + |
| 100 | +assert.doesNotThrow(() => { |
| 101 | + assert.throws(() => { |
| 102 | + throw new errors.TypeError('TEST_ERROR_1', 'a'); |
| 103 | + }, common.expectsError('TEST_ERROR_1', Error)); |
| 104 | +}); |
| 105 | + |
| 106 | +assert.throws(() => { |
| 107 | + assert.throws(() => { |
| 108 | + throw new errors.TypeError('TEST_ERROR_1', 'a'); |
| 109 | + }, common.expectsError('TEST_ERROR_1', RangeError)); |
| 110 | +}, /^AssertionError: error is not the expected type/); |
| 111 | + |
| 112 | +assert.throws(() => { |
| 113 | + assert.throws(() => { |
| 114 | + throw new errors.TypeError('TEST_ERROR_1', 'a'); |
| 115 | + }, common.expectsError('TEST_ERROR_1', TypeError, /^Error for testing 2/)); |
| 116 | +}, /^AssertionError: error.message does not match/); |
0 commit comments