11cb0ef41Sopenharmony_ci// Flags: --expose-internals
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_cirequire('../common');
41cb0ef41Sopenharmony_ciconst {
51cb0ef41Sopenharmony_ci  hijackStdout,
61cb0ef41Sopenharmony_ci  restoreStdout,
71cb0ef41Sopenharmony_ci} = require('../common/hijackstdio');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst assert = require('assert');
101cb0ef41Sopenharmony_ciconst errors = require('internal/errors');
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci// Turn off ANSI color formatting for this test file.
131cb0ef41Sopenharmony_ciconst { inspect } = require('util');
141cb0ef41Sopenharmony_ciinspect.defaultOptions.colors = false;
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_cierrors.E('TEST_ERROR_1', 'Error for testing purposes: %s',
171cb0ef41Sopenharmony_ci         Error, TypeError, RangeError);
181cb0ef41Sopenharmony_cierrors.E('TEST_ERROR_2', (a, b) => `${a} ${b}`, Error);
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci{
211cb0ef41Sopenharmony_ci  const err = new errors.codes.TEST_ERROR_1('test');
221cb0ef41Sopenharmony_ci  assert(err instanceof Error);
231cb0ef41Sopenharmony_ci  assert.strictEqual(err.name, 'Error');
241cb0ef41Sopenharmony_ci  assert.strictEqual(err.message, 'Error for testing purposes: test');
251cb0ef41Sopenharmony_ci  assert.strictEqual(err.code, 'TEST_ERROR_1');
261cb0ef41Sopenharmony_ci}
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci{
291cb0ef41Sopenharmony_ci  const err = new errors.codes.TEST_ERROR_1.TypeError('test');
301cb0ef41Sopenharmony_ci  assert(err instanceof TypeError);
311cb0ef41Sopenharmony_ci  assert.strictEqual(err.name, 'TypeError');
321cb0ef41Sopenharmony_ci  assert.strictEqual(err.message, 'Error for testing purposes: test');
331cb0ef41Sopenharmony_ci  assert.strictEqual(err.code, 'TEST_ERROR_1');
341cb0ef41Sopenharmony_ci}
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci{
371cb0ef41Sopenharmony_ci  const err = new errors.codes.TEST_ERROR_1.RangeError('test');
381cb0ef41Sopenharmony_ci  assert(err instanceof RangeError);
391cb0ef41Sopenharmony_ci  assert.strictEqual(err.name, 'RangeError');
401cb0ef41Sopenharmony_ci  assert.strictEqual(err.message, 'Error for testing purposes: test');
411cb0ef41Sopenharmony_ci  assert.strictEqual(err.code, 'TEST_ERROR_1');
421cb0ef41Sopenharmony_ci}
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci{
451cb0ef41Sopenharmony_ci  const err = new errors.codes.TEST_ERROR_2('abc', 'xyz');
461cb0ef41Sopenharmony_ci  assert(err instanceof Error);
471cb0ef41Sopenharmony_ci  assert.strictEqual(err.name, 'Error');
481cb0ef41Sopenharmony_ci  assert.strictEqual(err.message, 'abc xyz');
491cb0ef41Sopenharmony_ci  assert.strictEqual(err.code, 'TEST_ERROR_2');
501cb0ef41Sopenharmony_ci}
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci{
531cb0ef41Sopenharmony_ci  assert.throws(
541cb0ef41Sopenharmony_ci    () => new errors.codes.TEST_ERROR_1(),
551cb0ef41Sopenharmony_ci    {
561cb0ef41Sopenharmony_ci      message: /^Code: TEST_ERROR_1; The provided arguments length \(0\) does not match the required ones \(1\)\./,
571cb0ef41Sopenharmony_ci      name: 'Error',
581cb0ef41Sopenharmony_ci      code: 'ERR_INTERNAL_ASSERTION'
591cb0ef41Sopenharmony_ci    }
601cb0ef41Sopenharmony_ci  );
611cb0ef41Sopenharmony_ci}
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci// Tests for common.expectsError
641cb0ef41Sopenharmony_ciassert.throws(() => {
651cb0ef41Sopenharmony_ci  throw new errors.codes.TEST_ERROR_1.TypeError('a');
661cb0ef41Sopenharmony_ci}, { code: 'TEST_ERROR_1' });
671cb0ef41Sopenharmony_ciassert.throws(() => {
681cb0ef41Sopenharmony_ci  throw new errors.codes.TEST_ERROR_1.TypeError('a');
691cb0ef41Sopenharmony_ci}, { code: 'TEST_ERROR_1',
701cb0ef41Sopenharmony_ci     name: 'TypeError',
711cb0ef41Sopenharmony_ci     message: /^Error for testing/ });
721cb0ef41Sopenharmony_ciassert.throws(() => {
731cb0ef41Sopenharmony_ci  throw new errors.codes.TEST_ERROR_1.TypeError('a');
741cb0ef41Sopenharmony_ci}, { code: 'TEST_ERROR_1', name: 'TypeError' });
751cb0ef41Sopenharmony_ciassert.throws(() => {
761cb0ef41Sopenharmony_ci  throw new errors.codes.TEST_ERROR_1.TypeError('a');
771cb0ef41Sopenharmony_ci}, {
781cb0ef41Sopenharmony_ci  code: 'TEST_ERROR_1',
791cb0ef41Sopenharmony_ci  name: 'TypeError',
801cb0ef41Sopenharmony_ci  message: 'Error for testing purposes: a'
811cb0ef41Sopenharmony_ci});
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci// Test that `code` property is mutable and that changing it does not change the
841cb0ef41Sopenharmony_ci// name.
851cb0ef41Sopenharmony_ci{
861cb0ef41Sopenharmony_ci  const myError = new errors.codes.TEST_ERROR_1('foo');
871cb0ef41Sopenharmony_ci  assert.strictEqual(myError.code, 'TEST_ERROR_1');
881cb0ef41Sopenharmony_ci  assert.strictEqual(Object.hasOwn(myError, 'code'), true);
891cb0ef41Sopenharmony_ci  assert.strictEqual(Object.hasOwn(myError, 'name'), false);
901cb0ef41Sopenharmony_ci  assert.deepStrictEqual(Object.keys(myError), ['code']);
911cb0ef41Sopenharmony_ci  const initialName = myError.name;
921cb0ef41Sopenharmony_ci  myError.code = 'FHQWHGADS';
931cb0ef41Sopenharmony_ci  assert.strictEqual(myError.code, 'FHQWHGADS');
941cb0ef41Sopenharmony_ci  assert.strictEqual(myError.name, initialName);
951cb0ef41Sopenharmony_ci  assert.deepStrictEqual(Object.keys(myError), ['code']);
961cb0ef41Sopenharmony_ci  assert.ok(!myError.name.includes('TEST_ERROR_1'));
971cb0ef41Sopenharmony_ci  assert.ok(!myError.name.includes('FHQWHGADS'));
981cb0ef41Sopenharmony_ci}
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_ci// Test that `name` is mutable and that changing it alters `toString()` but not
1011cb0ef41Sopenharmony_ci// `console.log()` results, which is the behavior of `Error` objects in the
1021cb0ef41Sopenharmony_ci// browser. Note that `name` becomes enumerable after being assigned.
1031cb0ef41Sopenharmony_ci{
1041cb0ef41Sopenharmony_ci  const myError = new errors.codes.TEST_ERROR_1('foo');
1051cb0ef41Sopenharmony_ci  assert.deepStrictEqual(Object.keys(myError), ['code']);
1061cb0ef41Sopenharmony_ci  const initialToString = myError.toString();
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_ci  myError.name = 'Fhqwhgads';
1091cb0ef41Sopenharmony_ci  assert.deepStrictEqual(Object.keys(myError), ['code', 'name']);
1101cb0ef41Sopenharmony_ci  assert.notStrictEqual(myError.toString(), initialToString);
1111cb0ef41Sopenharmony_ci}
1121cb0ef41Sopenharmony_ci
1131cb0ef41Sopenharmony_ci// Test that `message` is mutable and that changing it alters `toString()` but
1141cb0ef41Sopenharmony_ci// not `console.log()` results, which is the behavior of `Error` objects in the
1151cb0ef41Sopenharmony_ci// browser. Note that `message` remains non-enumerable after being assigned.
1161cb0ef41Sopenharmony_ci{
1171cb0ef41Sopenharmony_ci  let initialConsoleLog = '';
1181cb0ef41Sopenharmony_ci  hijackStdout((data) => { initialConsoleLog += data; });
1191cb0ef41Sopenharmony_ci  const myError = new errors.codes.TEST_ERROR_1('foo');
1201cb0ef41Sopenharmony_ci  assert.deepStrictEqual(Object.keys(myError), ['code']);
1211cb0ef41Sopenharmony_ci  const initialToString = myError.toString();
1221cb0ef41Sopenharmony_ci  console.log(myError);
1231cb0ef41Sopenharmony_ci  assert.notStrictEqual(initialConsoleLog, '');
1241cb0ef41Sopenharmony_ci
1251cb0ef41Sopenharmony_ci  restoreStdout();
1261cb0ef41Sopenharmony_ci
1271cb0ef41Sopenharmony_ci  let subsequentConsoleLog = '';
1281cb0ef41Sopenharmony_ci  hijackStdout((data) => { subsequentConsoleLog += data; });
1291cb0ef41Sopenharmony_ci  myError.message = 'Fhqwhgads';
1301cb0ef41Sopenharmony_ci  assert.deepStrictEqual(Object.keys(myError), ['code']);
1311cb0ef41Sopenharmony_ci  assert.notStrictEqual(myError.toString(), initialToString);
1321cb0ef41Sopenharmony_ci  console.log(myError);
1331cb0ef41Sopenharmony_ci  assert.strictEqual(subsequentConsoleLog, initialConsoleLog);
1341cb0ef41Sopenharmony_ci
1351cb0ef41Sopenharmony_ci  restoreStdout();
1361cb0ef41Sopenharmony_ci}
137