-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fs: aggregate errors in fsPromises to avoid error swallowing
Add AggregateError support to fsPromises, instead of swallowing errors if fs.close throws. PR-URL: #38259 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
- Loading branch information
Showing
4 changed files
with
219 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
test/parallel/test-fs-promises-file-handle-aggregate-errors.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
'use strict'; | ||
// Flags: --expose-internals | ||
|
||
const common = require('../common'); | ||
const tmpdir = require('../common/tmpdir'); | ||
|
||
// The following tests validate aggregate errors are thrown correctly | ||
// when both an operation and close throw. | ||
|
||
const path = require('path'); | ||
const { | ||
readFile, | ||
writeFile, | ||
truncate, | ||
lchmod, | ||
} = require('fs/promises'); | ||
const { | ||
FileHandle, | ||
} = require('internal/fs/promises'); | ||
|
||
const assert = require('assert'); | ||
const originalFd = Object.getOwnPropertyDescriptor(FileHandle.prototype, 'fd'); | ||
|
||
let count = 0; | ||
async function createFile() { | ||
const filePath = path.join(tmpdir.path, `aggregate_errors_${++count}.txt`); | ||
await writeFile(filePath, 'content'); | ||
return filePath; | ||
} | ||
|
||
async function checkAggregateError(op) { | ||
try { | ||
const filePath = await createFile(); | ||
Object.defineProperty(FileHandle.prototype, 'fd', { | ||
get: function() { | ||
// Close is set by using a setter, | ||
// so it needs to be set on the instance. | ||
const originalClose = this.close; | ||
this.close = async () => { | ||
// close the file | ||
await originalClose.call(this); | ||
const closeError = new Error('CLOSE_ERROR'); | ||
closeError.code = 456; | ||
throw closeError; | ||
}; | ||
const opError = new Error('INTERNAL_ERROR'); | ||
opError.code = 123; | ||
throw opError; | ||
} | ||
}); | ||
|
||
await assert.rejects(op(filePath), common.mustCall((err) => { | ||
assert.strictEqual(err.name, 'AggregateError'); | ||
assert.strictEqual(err.code, 123); | ||
assert.strictEqual(err.errors.length, 2); | ||
assert.strictEqual(err.errors[0].message, 'INTERNAL_ERROR'); | ||
assert.strictEqual(err.errors[1].message, 'CLOSE_ERROR'); | ||
return true; | ||
})); | ||
} finally { | ||
Object.defineProperty(FileHandle.prototype, 'fd', originalFd); | ||
} | ||
} | ||
(async function() { | ||
tmpdir.refresh(); | ||
await checkAggregateError((filePath) => truncate(filePath)); | ||
await checkAggregateError((filePath) => readFile(filePath)); | ||
await checkAggregateError((filePath) => writeFile(filePath, '123')); | ||
if (common.isOSX) { | ||
await checkAggregateError((filePath) => lchmod(filePath, 0o777)); | ||
} | ||
})().then(common.mustCall()); |
67 changes: 67 additions & 0 deletions
67
test/parallel/test-fs-promises-file-handle-close-errors.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
'use strict'; | ||
// Flags: --expose-internals | ||
|
||
const common = require('../common'); | ||
const tmpdir = require('../common/tmpdir'); | ||
|
||
// The following tests validate aggregate errors are thrown correctly | ||
// when both an operation and close throw. | ||
|
||
const path = require('path'); | ||
const { | ||
readFile, | ||
writeFile, | ||
truncate, | ||
lchmod, | ||
} = require('fs/promises'); | ||
const { | ||
FileHandle, | ||
} = require('internal/fs/promises'); | ||
|
||
const assert = require('assert'); | ||
const originalFd = Object.getOwnPropertyDescriptor(FileHandle.prototype, 'fd'); | ||
|
||
let count = 0; | ||
async function createFile() { | ||
const filePath = path.join(tmpdir.path, `close_errors_${++count}.txt`); | ||
await writeFile(filePath, 'content'); | ||
return filePath; | ||
} | ||
|
||
async function checkCloseError(op) { | ||
try { | ||
const filePath = await createFile(); | ||
Object.defineProperty(FileHandle.prototype, 'fd', { | ||
get: function() { | ||
// Close is set by using a setter, | ||
// so it needs to be set on the instance. | ||
const originalClose = this.close; | ||
this.close = async () => { | ||
// close the file | ||
await originalClose.call(this); | ||
const closeError = new Error('CLOSE_ERROR'); | ||
closeError.code = 456; | ||
throw closeError; | ||
}; | ||
return originalFd.get.call(this); | ||
} | ||
}); | ||
|
||
await assert.rejects(op(filePath), { | ||
name: 'Error', | ||
message: 'CLOSE_ERROR', | ||
code: 456, | ||
}); | ||
} finally { | ||
Object.defineProperty(FileHandle.prototype, 'fd', originalFd); | ||
} | ||
} | ||
(async function() { | ||
tmpdir.refresh(); | ||
await checkCloseError((filePath) => truncate(filePath)); | ||
await checkCloseError((filePath) => readFile(filePath)); | ||
await checkCloseError((filePath) => writeFile(filePath, '123')); | ||
if (common.isOSX) { | ||
await checkCloseError((filePath) => lchmod(filePath, 0o777)); | ||
} | ||
})().then(common.mustCall()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
'use strict'; | ||
// Flags: --expose-internals | ||
|
||
const common = require('../common'); | ||
const tmpdir = require('../common/tmpdir'); | ||
|
||
// The following tests validate aggregate errors are thrown correctly | ||
// when both an operation and close throw. | ||
|
||
const path = require('path'); | ||
const { | ||
readFile, | ||
writeFile, | ||
truncate, | ||
lchmod, | ||
} = require('fs/promises'); | ||
const { | ||
FileHandle, | ||
} = require('internal/fs/promises'); | ||
|
||
const assert = require('assert'); | ||
const originalFd = Object.getOwnPropertyDescriptor(FileHandle.prototype, 'fd'); | ||
|
||
let count = 0; | ||
async function createFile() { | ||
const filePath = path.join(tmpdir.path, `op_errors_${++count}.txt`); | ||
await writeFile(filePath, 'content'); | ||
return filePath; | ||
} | ||
|
||
async function checkOperationError(op) { | ||
try { | ||
const filePath = await createFile(); | ||
Object.defineProperty(FileHandle.prototype, 'fd', { | ||
get: function() { | ||
// Verify that close is called when an error is thrown | ||
this.close = common.mustCall(this.close); | ||
const opError = new Error('INTERNAL_ERROR'); | ||
opError.code = 123; | ||
throw opError; | ||
} | ||
}); | ||
|
||
await assert.rejects(op(filePath), { | ||
name: 'Error', | ||
message: 'INTERNAL_ERROR', | ||
code: 123, | ||
}); | ||
} finally { | ||
Object.defineProperty(FileHandle.prototype, 'fd', originalFd); | ||
} | ||
} | ||
(async function() { | ||
tmpdir.refresh(); | ||
await checkOperationError((filePath) => truncate(filePath)); | ||
await checkOperationError((filePath) => readFile(filePath)); | ||
await checkOperationError((filePath) => writeFile(filePath, '123')); | ||
if (common.isOSX) { | ||
await checkOperationError((filePath) => lchmod(filePath, 0o777)); | ||
} | ||
})().then(common.mustCall()); |