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, `close_errors_${++count}.txt`); 271cb0ef41Sopenharmony_ci await writeFile(filePath, 'content'); 281cb0ef41Sopenharmony_ci return filePath; 291cb0ef41Sopenharmony_ci} 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_ciasync function checkCloseError(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 return originalFd.get.call(this); 471cb0ef41Sopenharmony_ci } 481cb0ef41Sopenharmony_ci }); 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_ci await assert.rejects(op(filePath), { 511cb0ef41Sopenharmony_ci name: 'Error', 521cb0ef41Sopenharmony_ci message: 'CLOSE_ERROR', 531cb0ef41Sopenharmony_ci code: 456, 541cb0ef41Sopenharmony_ci }); 551cb0ef41Sopenharmony_ci } finally { 561cb0ef41Sopenharmony_ci Object.defineProperty(FileHandle.prototype, 'fd', originalFd); 571cb0ef41Sopenharmony_ci } 581cb0ef41Sopenharmony_ci} 591cb0ef41Sopenharmony_ci(async function() { 601cb0ef41Sopenharmony_ci tmpdir.refresh(); 611cb0ef41Sopenharmony_ci await checkCloseError((filePath) => truncate(filePath)); 621cb0ef41Sopenharmony_ci await checkCloseError((filePath) => readFile(filePath)); 631cb0ef41Sopenharmony_ci await checkCloseError((filePath) => writeFile(filePath, '123')); 641cb0ef41Sopenharmony_ci if (common.isOSX) { 651cb0ef41Sopenharmony_ci await checkCloseError((filePath) => lchmod(filePath, 0o777)); 661cb0ef41Sopenharmony_ci } 671cb0ef41Sopenharmony_ci})().then(common.mustCall()); 68