From 55961b85ea38ec18f0126b121d9f4f8500bd4409 Mon Sep 17 00:00:00 2001 From: Nitzan Uziely Date: Sat, 17 Apr 2021 02:13:41 +0300 Subject: [PATCH] fixup! fs: aggregate errors in fsPromises to avoid error swallowing --- lib/internal/fs/promises.js | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/lib/internal/fs/promises.js b/lib/internal/fs/promises.js index 39ab2f8be31b829..a63065137513060 100644 --- a/lib/internal/fs/promises.js +++ b/lib/internal/fs/promises.js @@ -10,6 +10,7 @@ const { PromisePrototypeFinally, PromisePrototypeThen, PromiseResolve, + PromiseReject, SafeArrayIterator, Symbol, Uint8Array, @@ -252,24 +253,16 @@ class FileHandle extends EventEmitterMixin(JSTransferable) { } async function handleFdClose(fileOpPromise, closeFunc) { - let opError; - try { - return await fileOpPromise; - } catch (err) { - opError = err; - throw err; - } finally { - try { - await closeFunc(); - } catch (closeError) { - if (!opError) { - // eslint-disable-next-line no-unsafe-finally - throw closeError; - } - // eslint-disable-next-line no-unsafe-finally - throw aggregateTwoErrors(closeError, opError); - } - } + return PromisePrototypeThen( + fileOpPromise, + (result) => PromisePrototypeThen(closeFunc(), () => result), + (opError) => + PromisePrototypeThen( + closeFunc(), + () => PromiseReject(opError), + (closeError) => PromiseReject(aggregateTwoErrors(closeError, opError)) + ) + ); } async function fsCall(fn, handle, ...args) {