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, `op_errors_${++count}.txt`); 271cb0ef41Sopenharmony_ci await writeFile(filePath, 'content'); 281cb0ef41Sopenharmony_ci return filePath; 291cb0ef41Sopenharmony_ci} 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_ciasync function checkOperationError(op) { 321cb0ef41Sopenharmony_ci try { 331cb0ef41Sopenharmony_ci const filePath = await createFile(); 341cb0ef41Sopenharmony_ci Object.defineProperty(FileHandle.prototype, 'fd', { 351cb0ef41Sopenharmony_ci get: function() { 361cb0ef41Sopenharmony_ci // Verify that close is called when an error is thrown 371cb0ef41Sopenharmony_ci this.close = common.mustCall(this.close); 381cb0ef41Sopenharmony_ci const opError = new Error('INTERNAL_ERROR'); 391cb0ef41Sopenharmony_ci opError.code = 123; 401cb0ef41Sopenharmony_ci throw opError; 411cb0ef41Sopenharmony_ci } 421cb0ef41Sopenharmony_ci }); 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_ci await assert.rejects(op(filePath), { 451cb0ef41Sopenharmony_ci name: 'Error', 461cb0ef41Sopenharmony_ci message: 'INTERNAL_ERROR', 471cb0ef41Sopenharmony_ci code: 123, 481cb0ef41Sopenharmony_ci }); 491cb0ef41Sopenharmony_ci } finally { 501cb0ef41Sopenharmony_ci Object.defineProperty(FileHandle.prototype, 'fd', originalFd); 511cb0ef41Sopenharmony_ci } 521cb0ef41Sopenharmony_ci} 531cb0ef41Sopenharmony_ci(async function() { 541cb0ef41Sopenharmony_ci tmpdir.refresh(); 551cb0ef41Sopenharmony_ci await checkOperationError((filePath) => truncate(filePath)); 561cb0ef41Sopenharmony_ci await checkOperationError((filePath) => readFile(filePath)); 571cb0ef41Sopenharmony_ci await checkOperationError((filePath) => writeFile(filePath, '123')); 581cb0ef41Sopenharmony_ci if (common.isOSX) { 591cb0ef41Sopenharmony_ci await checkOperationError((filePath) => lchmod(filePath, 0o777)); 601cb0ef41Sopenharmony_ci } 611cb0ef41Sopenharmony_ci})().then(common.mustCall()); 62