• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Flags: --expose-internals
2'use strict';
3require('../common');
4const assert = require('assert');
5const { ERR_INVALID_ARG_TYPE } = require('internal/errors').codes;
6const { serializeError, deserializeError } = require('internal/error_serdes');
7
8function cycle(err) {
9  return deserializeError(serializeError(err));
10}
11
12assert.strictEqual(cycle(0), 0);
13assert.strictEqual(cycle(-1), -1);
14assert.strictEqual(cycle(1.4), 1.4);
15assert.strictEqual(cycle(null), null);
16assert.strictEqual(cycle(undefined), undefined);
17assert.strictEqual(cycle('foo'), 'foo');
18
19let err = new Error('foo');
20for (let i = 0; i < 10; i++) {
21  assert(err instanceof Error);
22  assert(Object.prototype.toString.call(err), '[object Error]');
23  assert.strictEqual(err.name, 'Error');
24  assert.strictEqual(err.message, 'foo');
25  assert(/^Error: foo\n/.test(err.stack));
26
27  const prev = err;
28  err = cycle(err);
29  assert.deepStrictEqual(err, prev);
30}
31
32assert.strictEqual(cycle(new RangeError('foo')).name, 'RangeError');
33assert.strictEqual(cycle(new TypeError('foo')).name, 'TypeError');
34assert.strictEqual(cycle(new ReferenceError('foo')).name, 'ReferenceError');
35assert.strictEqual(cycle(new URIError('foo')).name, 'URIError');
36assert.strictEqual(cycle(new EvalError('foo')).name, 'EvalError');
37assert.strictEqual(cycle(new SyntaxError('foo')).name, 'SyntaxError');
38
39class SubError extends Error {}
40
41assert.strictEqual(cycle(new SubError('foo')).name, 'Error');
42
43assert.deepStrictEqual(cycle({ message: 'foo' }), { message: 'foo' });
44assert.strictEqual(cycle(Function), '[Function: Function]');
45
46{
47  const err = new ERR_INVALID_ARG_TYPE('object', 'Object', 42);
48  assert(/^TypeError \[ERR_INVALID_ARG_TYPE\]:/.test(err));
49  assert.strictEqual(err.name, 'TypeError');
50  assert.strictEqual(err.code, 'ERR_INVALID_ARG_TYPE');
51}
52
53{
54  let called = false;
55  class DynamicError extends Error {
56    get type() {
57      called = true;
58      return 'dynamic';
59    }
60
61    get shouldIgnoreError() {
62      throw new Error();
63    }
64  }
65
66  serializeError(new DynamicError());
67  assert.deepStrictEqual(called, true);
68}
69