Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fixup! fs: aggregate errors in fsPromises to avoid error swallowing
  • Loading branch information
Linkgoron committed Apr 23, 2021
commit 83f69a3765616b9c71395c351116f91377bdb530
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ async function checkAggregateError(op) {
}
});

await op(filePath).catch(common.mustCall((err) => {
assert.strictEqual(err.constructor.name, 'AggregateError');
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);
Expand Down
20 changes: 10 additions & 10 deletions test/parallel/test-fs-promises-file-handle-close-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async function createFile() {
return filePath;
}

async function checkAggregateError(op) {
async function checkCloseError(op) {
try {
const filePath = await createFile();
Object.defineProperty(FileHandle.prototype, 'fd', {
Expand All @@ -48,21 +48,21 @@ async function checkAggregateError(op) {
}
});

await op(filePath).catch(common.mustCall((err) => {
assert.strictEqual(err.constructor.name, 'Error');
assert.strictEqual(err.message, 'CLOSE_ERROR');
assert.strictEqual(err.code, 456);
}));
await assert.rejects(op(filePath), {
name: 'Error',
message: 'CLOSE_ERROR',
code: 456,
});
} 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'));
await checkCloseError((filePath) => truncate(filePath));
await checkCloseError((filePath) => readFile(filePath));
await checkCloseError((filePath) => writeFile(filePath, '123'));
if (common.isOSX) {
await checkAggregateError((filePath) => lchmod(filePath, 0o777));
await checkCloseError((filePath) => lchmod(filePath, 0o777));
}
})().then(common.mustCall());
28 changes: 12 additions & 16 deletions test/parallel/test-fs-promises-file-handle-op-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,38 +29,34 @@ async function createFile() {
return filePath;
}

async function checkAggregateError(op) {
async function checkOperationError(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 = common.mustCall(function(...args) {
return originalClose.apply(this, args);
});
// 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 op(filePath).catch(common.mustCall((err) => {
assert.strictEqual(err.constructor.name, 'Error');
assert.strictEqual(err.message, 'INTERNAL_ERROR');
assert.strictEqual(err.code, 123);
}));
await assert.rejects(op(filePath), {
name: 'Error',
message: 'INTERNAL_ERROR',
code: 123,
});
} 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'));
await checkOperationError((filePath) => truncate(filePath));
await checkOperationError((filePath) => readFile(filePath));
await checkOperationError((filePath) => writeFile(filePath, '123'));
if (common.isOSX) {
await checkAggregateError((filePath) => lchmod(filePath, 0o777));
await checkOperationError((filePath) => lchmod(filePath, 0o777));
}
})().then(common.mustCall());