Delete temporary file when error in writing to stream

This commit is contained in:
Simon Detheridge 2020-01-06 14:43:23 +00:00
parent 039bec02f7
commit 25f1c2bfc4
2 changed files with 52 additions and 9 deletions

View file

@ -32,6 +32,8 @@ async function writeStream(stream, key) {
logger.log({ fsPath }, 'finished writing file locally')
return fsPath
} catch (err) {
await deleteFile(fsPath)
logger.err({ err, fsPath }, 'problem writing file locally')
throw new WriteError({
message: 'problem writing file locally',
@ -45,7 +47,16 @@ async function deleteFile(fsPath) {
return
}
logger.log({ fsPath }, 'removing local temp file')
await promisify(fs.unlink)(fsPath)
try {
await promisify(fs.unlink)(fsPath)
} catch (err) {
if (err.code !== 'ENOENT') {
throw new WriteError({
message: 'failed to delete file',
info: { fsPath }
}).withCause(err)
}
}
}
function _getPath(key) {

View file

@ -49,6 +49,26 @@ describe('LocalFileWriter', function() {
done()
})
})
describe('when there is an error', function() {
const error = new Error('not enough ketchup')
beforeEach(function() {
stream.pipeline.yields(error)
})
it('should wrap the error', function() {
LocalFileWriter.writeStream(readStream, filename, err => {
expect(err).to.exist
expect(err.cause).to.equal(error)
})
})
it('should delete the temporary file', function() {
LocalFileWriter.writeStream(readStream, filename, () => {
expect(fs.unlink).to.have.been.calledWith(fsPath)
})
})
})
})
describe('deleteFile', function() {
@ -60,14 +80,6 @@ describe('LocalFileWriter', function() {
})
})
it('should not do anything if called with an empty path', function(done) {
fs.unlink = sinon.stub().yields(new Error('failed to reticulate splines'))
LocalFileWriter.deleteFile(fsPath, err => {
expect(err).to.exist
done()
})
})
it('should not call unlink with an empty path', function(done) {
LocalFileWriter.deleteFile('', err => {
expect(err).not.to.exist
@ -75,5 +87,25 @@ describe('LocalFileWriter', function() {
done()
})
})
it('should not throw a error if the file does not exist', function(done) {
const error = new Error('file not found')
error.code = 'ENOENT'
fs.unlink = sinon.stub().yields(error)
LocalFileWriter.deleteFile(fsPath, err => {
expect(err).not.to.exist
done()
})
})
it('should wrap the error', function(done) {
const error = new Error('failed to reticulate splines')
fs.unlink = sinon.stub().yields(error)
LocalFileWriter.deleteFile(fsPath, err => {
expect(err).to.exist
expect(err.cause).to.equal(error)
done()
})
})
})
})