1'use strict'; 2 3const common = require('../../common'); 4const test_error = require(`./build/${common.buildType}/test_error`); 5const assert = require('assert'); 6const theError = new Error('Some error'); 7const theTypeError = new TypeError('Some type error'); 8const theSyntaxError = new SyntaxError('Some syntax error'); 9const theRangeError = new RangeError('Some type error'); 10const theReferenceError = new ReferenceError('Some reference error'); 11const theURIError = new URIError('Some URI error'); 12const theEvalError = new EvalError('Some eval error'); 13 14class MyError extends Error { } 15const myError = new MyError('Some MyError'); 16 17// Test that native error object is correctly classed 18assert.strictEqual(test_error.checkError(theError), true); 19 20// Test that native type error object is correctly classed 21assert.strictEqual(test_error.checkError(theTypeError), true); 22 23// Test that native syntax error object is correctly classed 24assert.strictEqual(test_error.checkError(theSyntaxError), true); 25 26// Test that native range error object is correctly classed 27assert.strictEqual(test_error.checkError(theRangeError), true); 28 29// Test that native reference error object is correctly classed 30assert.strictEqual(test_error.checkError(theReferenceError), true); 31 32// Test that native URI error object is correctly classed 33assert.strictEqual(test_error.checkError(theURIError), true); 34 35// Test that native eval error object is correctly classed 36assert.strictEqual(test_error.checkError(theEvalError), true); 37 38// Test that class derived from native error is correctly classed 39assert.strictEqual(test_error.checkError(myError), true); 40 41// Test that non-error object is correctly classed 42assert.strictEqual(test_error.checkError({}), false); 43 44// Test that non-error primitive is correctly classed 45assert.strictEqual(test_error.checkError('non-object'), false); 46 47assert.throws(() => { 48 test_error.throwExistingError(); 49}, /^Error: existing error$/); 50 51assert.throws(() => { 52 test_error.throwError(); 53}, /^Error: error$/); 54 55assert.throws(() => { 56 test_error.throwRangeError(); 57}, /^RangeError: range error$/); 58 59assert.throws(() => { 60 test_error.throwTypeError(); 61}, /^TypeError: type error$/); 62 63assert.throws(() => { 64 test_error.throwSyntaxError(); 65}, /^SyntaxError: syntax error$/); 66 67[42, {}, [], Symbol('xyzzy'), true, 'ball', undefined, null, NaN] 68 .forEach((value) => assert.throws( 69 () => test_error.throwArbitrary(value), 70 (err) => { 71 assert.strictEqual(err, value); 72 return true; 73 }, 74 )); 75 76assert.throws( 77 () => test_error.throwErrorCode(), 78 { 79 code: 'ERR_TEST_CODE', 80 message: 'Error [error]', 81 }); 82 83assert.throws( 84 () => test_error.throwRangeErrorCode(), 85 { 86 code: 'ERR_TEST_CODE', 87 message: 'RangeError [range error]', 88 }); 89 90assert.throws( 91 () => test_error.throwTypeErrorCode(), 92 { 93 code: 'ERR_TEST_CODE', 94 message: 'TypeError [type error]', 95 }); 96 97assert.throws( 98 () => test_error.throwSyntaxErrorCode(), 99 { 100 code: 'ERR_TEST_CODE', 101 message: 'SyntaxError [syntax error]', 102 }); 103 104let error = test_error.createError(); 105assert.ok(error instanceof Error, 'expected error to be an instance of Error'); 106assert.strictEqual(error.message, 'error'); 107 108error = test_error.createRangeError(); 109assert.ok(error instanceof RangeError, 110 'expected error to be an instance of RangeError'); 111assert.strictEqual(error.message, 'range error'); 112 113error = test_error.createTypeError(); 114assert.ok(error instanceof TypeError, 115 'expected error to be an instance of TypeError'); 116assert.strictEqual(error.message, 'type error'); 117 118error = test_error.createSyntaxError(); 119assert.ok(error instanceof SyntaxError, 120 'expected error to be an instance of SyntaxError'); 121assert.strictEqual(error.message, 'syntax error'); 122 123error = test_error.createErrorCode(); 124assert.ok(error instanceof Error, 'expected error to be an instance of Error'); 125assert.strictEqual(error.code, 'ERR_TEST_CODE'); 126assert.strictEqual(error.message, 'Error [error]'); 127assert.strictEqual(error.name, 'Error'); 128 129error = test_error.createRangeErrorCode(); 130assert.ok(error instanceof RangeError, 131 'expected error to be an instance of RangeError'); 132assert.strictEqual(error.message, 'RangeError [range error]'); 133assert.strictEqual(error.code, 'ERR_TEST_CODE'); 134assert.strictEqual(error.name, 'RangeError'); 135 136error = test_error.createTypeErrorCode(); 137assert.ok(error instanceof TypeError, 138 'expected error to be an instance of TypeError'); 139assert.strictEqual(error.message, 'TypeError [type error]'); 140assert.strictEqual(error.code, 'ERR_TEST_CODE'); 141assert.strictEqual(error.name, 'TypeError'); 142 143error = test_error.createSyntaxErrorCode(); 144assert.ok(error instanceof SyntaxError, 145 'expected error to be an instance of SyntaxError'); 146assert.strictEqual(error.message, 'SyntaxError [syntax error]'); 147assert.strictEqual(error.code, 'ERR_TEST_CODE'); 148assert.strictEqual(error.name, 'SyntaxError'); 149