• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Flags: --expose-internals
2'use strict';
3
4require('../common');
5const assert = require('assert');
6const { types } = require('util');
7const { isError } = require('internal/util');
8const vm = require('vm');
9
10// Special cased errors. Test the internal function which is used in
11// `util.inspect()`, the `repl` and maybe more. This verifies that errors from
12// different realms, and non native instances of error are properly detected as
13// error while definitely false ones are not detected. This is different than
14// the public `util.isError()` function which falsy detects the fake errors as
15// actual errors.
16{
17  const fake = { [Symbol.toStringTag]: 'Error' };
18  assert(!types.isNativeError(fake));
19  assert(!(fake instanceof Error));
20  assert(!isError(fake));
21
22  const err = new Error('test');
23  const newErr = Object.create(
24    Object.getPrototypeOf(err),
25    Object.getOwnPropertyDescriptors(err));
26  Object.defineProperty(err, 'message', { value: err.message });
27  assert(types.isNativeError(err));
28  assert(!types.isNativeError(newErr));
29  assert(newErr instanceof Error);
30  assert(isError(newErr));
31
32  const context = vm.createContext({});
33  const differentRealmErr = vm.runInContext('new Error()', context);
34  assert(types.isNativeError(differentRealmErr));
35  assert(!(differentRealmErr instanceof Error));
36  assert(isError(differentRealmErr));
37}
38