• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
63[42, {}, [], Symbol('xyzzy'), true, 'ball', undefined, null, NaN]
64  .forEach((value) => assert.throws(
65    () => test_error.throwArbitrary(value),
66    (err) => {
67      assert.strictEqual(err, value);
68      return true;
69    }
70  ));
71
72assert.throws(
73  () => test_error.throwErrorCode(),
74  {
75    code: 'ERR_TEST_CODE',
76    message: 'Error [error]'
77  });
78
79assert.throws(
80  () => test_error.throwRangeErrorCode(),
81  {
82    code: 'ERR_TEST_CODE',
83    message: 'RangeError [range error]'
84  });
85
86assert.throws(
87  () => test_error.throwTypeErrorCode(),
88  {
89    code: 'ERR_TEST_CODE',
90    message: 'TypeError [type error]'
91  });
92
93let error = test_error.createError();
94assert.ok(error instanceof Error, 'expected error to be an instance of Error');
95assert.strictEqual(error.message, 'error');
96
97error = test_error.createRangeError();
98assert.ok(error instanceof RangeError,
99          'expected error to be an instance of RangeError');
100assert.strictEqual(error.message, 'range error');
101
102error = test_error.createTypeError();
103assert.ok(error instanceof TypeError,
104          'expected error to be an instance of TypeError');
105assert.strictEqual(error.message, 'type error');
106
107error = test_error.createErrorCode();
108assert.ok(error instanceof Error, 'expected error to be an instance of Error');
109assert.strictEqual(error.code, 'ERR_TEST_CODE');
110assert.strictEqual(error.message, 'Error [error]');
111assert.strictEqual(error.name, 'Error');
112
113error = test_error.createRangeErrorCode();
114assert.ok(error instanceof RangeError,
115          'expected error to be an instance of RangeError');
116assert.strictEqual(error.message, 'RangeError [range error]');
117assert.strictEqual(error.code, 'ERR_TEST_CODE');
118assert.strictEqual(error.name, 'RangeError');
119
120error = test_error.createTypeErrorCode();
121assert.ok(error instanceof TypeError,
122          'expected error to be an instance of TypeError');
123assert.strictEqual(error.message, 'TypeError [type error]');
124assert.strictEqual(error.code, 'ERR_TEST_CODE');
125assert.strictEqual(error.name, 'TypeError');
126