11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci// Flags: --expose-internals
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciconst common = require('../common');
51cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci// The following tests validate aggregate errors are thrown correctly
81cb0ef41Sopenharmony_ci// when both an operation and close throw.
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst path = require('path');
111cb0ef41Sopenharmony_ciconst {
121cb0ef41Sopenharmony_ci  readFile,
131cb0ef41Sopenharmony_ci  writeFile,
141cb0ef41Sopenharmony_ci  truncate,
151cb0ef41Sopenharmony_ci  lchmod,
161cb0ef41Sopenharmony_ci} = require('fs/promises');
171cb0ef41Sopenharmony_ciconst {
181cb0ef41Sopenharmony_ci  FileHandle,
191cb0ef41Sopenharmony_ci} = require('internal/fs/promises');
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ciconst assert = require('assert');
221cb0ef41Sopenharmony_ciconst originalFd = Object.getOwnPropertyDescriptor(FileHandle.prototype, 'fd');
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_cilet count = 0;
251cb0ef41Sopenharmony_ciasync function createFile() {
261cb0ef41Sopenharmony_ci  const filePath = path.join(tmpdir.path, `aggregate_errors_${++count}.txt`);
271cb0ef41Sopenharmony_ci  await writeFile(filePath, 'content');
281cb0ef41Sopenharmony_ci  return filePath;
291cb0ef41Sopenharmony_ci}
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ciasync function checkAggregateError(op) {
321cb0ef41Sopenharmony_ci  try {
331cb0ef41Sopenharmony_ci    const filePath = await createFile();
341cb0ef41Sopenharmony_ci    Object.defineProperty(FileHandle.prototype, 'fd', {
351cb0ef41Sopenharmony_ci      get: function() {
361cb0ef41Sopenharmony_ci        // Close is set by using a setter,
371cb0ef41Sopenharmony_ci        // so it needs to be set on the instance.
381cb0ef41Sopenharmony_ci        const originalClose = this.close;
391cb0ef41Sopenharmony_ci        this.close = async () => {
401cb0ef41Sopenharmony_ci          // close the file
411cb0ef41Sopenharmony_ci          await originalClose.call(this);
421cb0ef41Sopenharmony_ci          const closeError = new Error('CLOSE_ERROR');
431cb0ef41Sopenharmony_ci          closeError.code = 456;
441cb0ef41Sopenharmony_ci          throw closeError;
451cb0ef41Sopenharmony_ci        };
461cb0ef41Sopenharmony_ci        const opError = new Error('INTERNAL_ERROR');
471cb0ef41Sopenharmony_ci        opError.code = 123;
481cb0ef41Sopenharmony_ci        throw opError;
491cb0ef41Sopenharmony_ci      }
501cb0ef41Sopenharmony_ci    });
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci    await assert.rejects(op(filePath), common.mustCall((err) => {
531cb0ef41Sopenharmony_ci      assert.strictEqual(err.name, 'AggregateError');
541cb0ef41Sopenharmony_ci      assert.strictEqual(err.code, 123);
551cb0ef41Sopenharmony_ci      assert.strictEqual(err.errors.length, 2);
561cb0ef41Sopenharmony_ci      assert.strictEqual(err.errors[0].message, 'INTERNAL_ERROR');
571cb0ef41Sopenharmony_ci      assert.strictEqual(err.errors[1].message, 'CLOSE_ERROR');
581cb0ef41Sopenharmony_ci      return true;
591cb0ef41Sopenharmony_ci    }));
601cb0ef41Sopenharmony_ci  } finally {
611cb0ef41Sopenharmony_ci    Object.defineProperty(FileHandle.prototype, 'fd', originalFd);
621cb0ef41Sopenharmony_ci  }
631cb0ef41Sopenharmony_ci}
641cb0ef41Sopenharmony_ci(async function() {
651cb0ef41Sopenharmony_ci  tmpdir.refresh();
661cb0ef41Sopenharmony_ci  await checkAggregateError((filePath) => truncate(filePath));
671cb0ef41Sopenharmony_ci  await checkAggregateError((filePath) => readFile(filePath));
681cb0ef41Sopenharmony_ci  await checkAggregateError((filePath) => writeFile(filePath, '123'));
691cb0ef41Sopenharmony_ci  if (common.isOSX) {
701cb0ef41Sopenharmony_ci    await checkAggregateError((filePath) => lchmod(filePath, 0o777));
711cb0ef41Sopenharmony_ci  }
721cb0ef41Sopenharmony_ci})().then(common.mustCall());
73