Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: catch unlink errors during cleanup #56

Merged
merged 1 commit into from
Nov 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 3 additions & 1 deletion lib/RollingFileWriteStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,9 @@ class RollingFileWriteStream extends Writable {

const deleteFiles = fileNames => {
debug(`deleteFiles: files to delete: ${fileNames}`);
return Promise.all(fileNames.map(f => fs.unlink(f)));
return Promise.all(fileNames.map(f => fs.unlink(f).catch((e) => {
debug(`deleteFiles: error when unlinking ${f}, ignoring. Error was ${e}`);
})));
};

module.exports = RollingFileWriteStream;
22 changes: 22 additions & 0 deletions test/RollingFileWriteStream-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1416,4 +1416,26 @@ describe("RollingFileWriteStream", () => {
s.currentFileStream.emit("error", new Error("oh no"));
});
});

describe("when deleting old files and there is an error", () => {
before(done => {
fs.ensureDir('/tmp/delete-test/logfile.log.2', done);
});

it("should not let errors bubble up", done => {
const s = new RollingFileWriteStream("/tmp/delete-test/logfile.log", {
maxSize: 10,
numToKeep: 1
});

s.write("length is 10", "utf8", () => {
// if there's an error during deletion, then done never gets called
s.write("length is 10", "utf8", done);
});
});

after(done => {
fs.remove('/tmp/delete-test', done);
})
});
});