11cb0ef41Sopenharmony_ci// Flags: --expose-internals
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_cirequire('../common');
51cb0ef41Sopenharmony_ciconst assert = require('assert');
61cb0ef41Sopenharmony_ciconst { E, SystemError, codes } = require('internal/errors');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciassert.throws(
91cb0ef41Sopenharmony_ci  () => { new SystemError(); },
101cb0ef41Sopenharmony_ci  {
111cb0ef41Sopenharmony_ci    name: 'TypeError',
121cb0ef41Sopenharmony_ci    message: "Cannot read properties of undefined (reading 'syscall')",
131cb0ef41Sopenharmony_ci  }
141cb0ef41Sopenharmony_ci);
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ciE('ERR_TEST', 'custom message', SystemError);
171cb0ef41Sopenharmony_ciconst { ERR_TEST } = codes;
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci{
201cb0ef41Sopenharmony_ci  const ctx = {
211cb0ef41Sopenharmony_ci    code: 'ETEST',
221cb0ef41Sopenharmony_ci    message: 'code message',
231cb0ef41Sopenharmony_ci    syscall: 'syscall_test',
241cb0ef41Sopenharmony_ci    path: '/str',
251cb0ef41Sopenharmony_ci    dest: '/str2'
261cb0ef41Sopenharmony_ci  };
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci  assert.throws(
291cb0ef41Sopenharmony_ci    () => { throw new ERR_TEST(ctx); },
301cb0ef41Sopenharmony_ci    {
311cb0ef41Sopenharmony_ci      code: 'ERR_TEST',
321cb0ef41Sopenharmony_ci      name: 'SystemError',
331cb0ef41Sopenharmony_ci      message: 'custom message: syscall_test returned ETEST (code message)' +
341cb0ef41Sopenharmony_ci               ' /str => /str2',
351cb0ef41Sopenharmony_ci      info: ctx
361cb0ef41Sopenharmony_ci    }
371cb0ef41Sopenharmony_ci  );
381cb0ef41Sopenharmony_ci}
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci{
411cb0ef41Sopenharmony_ci  const ctx = {
421cb0ef41Sopenharmony_ci    code: 'ETEST',
431cb0ef41Sopenharmony_ci    message: 'code message',
441cb0ef41Sopenharmony_ci    syscall: 'syscall_test',
451cb0ef41Sopenharmony_ci    path: Buffer.from('/buf'),
461cb0ef41Sopenharmony_ci    dest: '/str2'
471cb0ef41Sopenharmony_ci  };
481cb0ef41Sopenharmony_ci  assert.throws(
491cb0ef41Sopenharmony_ci    () => { throw new ERR_TEST(ctx); },
501cb0ef41Sopenharmony_ci    {
511cb0ef41Sopenharmony_ci      code: 'ERR_TEST',
521cb0ef41Sopenharmony_ci      name: 'SystemError',
531cb0ef41Sopenharmony_ci      message: 'custom message: syscall_test returned ETEST (code message)' +
541cb0ef41Sopenharmony_ci               ' /buf => /str2',
551cb0ef41Sopenharmony_ci      info: ctx
561cb0ef41Sopenharmony_ci    }
571cb0ef41Sopenharmony_ci  );
581cb0ef41Sopenharmony_ci}
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci{
611cb0ef41Sopenharmony_ci  const ctx = {
621cb0ef41Sopenharmony_ci    code: 'ETEST',
631cb0ef41Sopenharmony_ci    message: 'code message',
641cb0ef41Sopenharmony_ci    syscall: 'syscall_test',
651cb0ef41Sopenharmony_ci    path: Buffer.from('/buf'),
661cb0ef41Sopenharmony_ci    dest: Buffer.from('/buf2')
671cb0ef41Sopenharmony_ci  };
681cb0ef41Sopenharmony_ci  assert.throws(
691cb0ef41Sopenharmony_ci    () => { throw new ERR_TEST(ctx); },
701cb0ef41Sopenharmony_ci    {
711cb0ef41Sopenharmony_ci      code: 'ERR_TEST',
721cb0ef41Sopenharmony_ci      name: 'SystemError',
731cb0ef41Sopenharmony_ci      message: 'custom message: syscall_test returned ETEST (code message)' +
741cb0ef41Sopenharmony_ci               ' /buf => /buf2',
751cb0ef41Sopenharmony_ci      info: ctx
761cb0ef41Sopenharmony_ci    }
771cb0ef41Sopenharmony_ci  );
781cb0ef41Sopenharmony_ci}
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci{
811cb0ef41Sopenharmony_ci  const ctx = {
821cb0ef41Sopenharmony_ci    code: 'ERR',
831cb0ef41Sopenharmony_ci    errno: 123,
841cb0ef41Sopenharmony_ci    message: 'something happened',
851cb0ef41Sopenharmony_ci    syscall: 'syscall_test',
861cb0ef41Sopenharmony_ci    path: Buffer.from('a'),
871cb0ef41Sopenharmony_ci    dest: Buffer.from('b')
881cb0ef41Sopenharmony_ci  };
891cb0ef41Sopenharmony_ci  const err = new ERR_TEST(ctx);
901cb0ef41Sopenharmony_ci  assert.strictEqual(err.info, ctx);
911cb0ef41Sopenharmony_ci  assert.strictEqual(err.code, 'ERR_TEST');
921cb0ef41Sopenharmony_ci  err.code = 'test';
931cb0ef41Sopenharmony_ci  assert.strictEqual(err.code, 'test');
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_ci  // Test legacy properties. These shouldn't be used anymore
961cb0ef41Sopenharmony_ci  // but let us make sure they still work
971cb0ef41Sopenharmony_ci  assert.strictEqual(err.errno, 123);
981cb0ef41Sopenharmony_ci  assert.strictEqual(err.syscall, 'syscall_test');
991cb0ef41Sopenharmony_ci  assert.strictEqual(err.path, 'a');
1001cb0ef41Sopenharmony_ci  assert.strictEqual(err.dest, 'b');
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ci  // Make sure it's mutable
1031cb0ef41Sopenharmony_ci  err.code = 'test';
1041cb0ef41Sopenharmony_ci  err.errno = 321;
1051cb0ef41Sopenharmony_ci  err.syscall = 'test';
1061cb0ef41Sopenharmony_ci  err.path = 'path';
1071cb0ef41Sopenharmony_ci  err.dest = 'path';
1081cb0ef41Sopenharmony_ci
1091cb0ef41Sopenharmony_ci  assert.strictEqual(err.errno, 321);
1101cb0ef41Sopenharmony_ci  assert.strictEqual(err.syscall, 'test');
1111cb0ef41Sopenharmony_ci  assert.strictEqual(err.path, 'path');
1121cb0ef41Sopenharmony_ci  assert.strictEqual(err.dest, 'path');
1131cb0ef41Sopenharmony_ci}
1141cb0ef41Sopenharmony_ci
1151cb0ef41Sopenharmony_ci{
1161cb0ef41Sopenharmony_ci  const ctx = {
1171cb0ef41Sopenharmony_ci    code: 'ERR_TEST',
1181cb0ef41Sopenharmony_ci    message: 'Error occurred',
1191cb0ef41Sopenharmony_ci    syscall: 'syscall_test'
1201cb0ef41Sopenharmony_ci  };
1211cb0ef41Sopenharmony_ci  assert.throws(
1221cb0ef41Sopenharmony_ci    () => {
1231cb0ef41Sopenharmony_ci      const err = new ERR_TEST(ctx);
1241cb0ef41Sopenharmony_ci      err.name = 'Foobar';
1251cb0ef41Sopenharmony_ci      throw err;
1261cb0ef41Sopenharmony_ci    },
1271cb0ef41Sopenharmony_ci    {
1281cb0ef41Sopenharmony_ci      code: 'ERR_TEST',
1291cb0ef41Sopenharmony_ci      name: 'Foobar',
1301cb0ef41Sopenharmony_ci      message: 'custom message: syscall_test returned ERR_TEST ' +
1311cb0ef41Sopenharmony_ci               '(Error occurred)',
1321cb0ef41Sopenharmony_ci      info: ctx
1331cb0ef41Sopenharmony_ci    }
1341cb0ef41Sopenharmony_ci  );
1351cb0ef41Sopenharmony_ci}
136