11cb0ef41Sopenharmony_ci// Flags: --expose-internals
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_cirequire('../common');
51cb0ef41Sopenharmony_ciconst assert = require('assert');
61cb0ef41Sopenharmony_ciconst { types } = require('util');
71cb0ef41Sopenharmony_ciconst { isError } = require('internal/util');
81cb0ef41Sopenharmony_ciconst vm = require('vm');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci// Special cased errors. Test the internal function which is used in
111cb0ef41Sopenharmony_ci// `util.inspect()`, the `repl` and maybe more. This verifies that errors from
121cb0ef41Sopenharmony_ci// different realms, and non native instances of error are properly detected as
131cb0ef41Sopenharmony_ci// error while definitely false ones are not detected. This is different than
141cb0ef41Sopenharmony_ci// the public `util.isError()` function which falsy detects the fake errors as
151cb0ef41Sopenharmony_ci// actual errors.
161cb0ef41Sopenharmony_ci{
171cb0ef41Sopenharmony_ci  const fake = { [Symbol.toStringTag]: 'Error' };
181cb0ef41Sopenharmony_ci  assert(!types.isNativeError(fake));
191cb0ef41Sopenharmony_ci  assert(!(fake instanceof Error));
201cb0ef41Sopenharmony_ci  assert(!isError(fake));
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci  const err = new Error('test');
231cb0ef41Sopenharmony_ci  const newErr = Object.create(
241cb0ef41Sopenharmony_ci    Object.getPrototypeOf(err),
251cb0ef41Sopenharmony_ci    Object.getOwnPropertyDescriptors(err));
261cb0ef41Sopenharmony_ci  Object.defineProperty(err, 'message', { value: err.message });
271cb0ef41Sopenharmony_ci  assert(types.isNativeError(err));
281cb0ef41Sopenharmony_ci  assert(!types.isNativeError(newErr));
291cb0ef41Sopenharmony_ci  assert(newErr instanceof Error);
301cb0ef41Sopenharmony_ci  assert(isError(newErr));
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci  const context = vm.createContext({});
331cb0ef41Sopenharmony_ci  const differentRealmErr = vm.runInContext('new Error()', context);
341cb0ef41Sopenharmony_ci  assert(types.isNativeError(differentRealmErr));
351cb0ef41Sopenharmony_ci  assert(!(differentRealmErr instanceof Error));
361cb0ef41Sopenharmony_ci  assert(isError(differentRealmErr));
371cb0ef41Sopenharmony_ci}
38