diff --git a/src/storage/accessors/AtomicFileDataAccessor.ts b/src/storage/accessors/AtomicFileDataAccessor.ts index e92079d2df..1660c70160 100644 --- a/src/storage/accessors/AtomicFileDataAccessor.ts +++ b/src/storage/accessors/AtomicFileDataAccessor.ts @@ -48,8 +48,12 @@ export class AtomicFileDataAccessor extends FileDataAccessor implements AtomicDa await fsPromises.rename(tempFilePath, link.filePath); } catch (error: unknown) { // Delete the data already written - if ((await this.getStats(tempFilePath)).isFile()) { - await fsPromises.unlink(tempFilePath); + try { + if ((await this.getStats(tempFilePath)).isFile()) { + await fsPromises.unlink(tempFilePath); + } + } catch { + throw error; } throw error; } diff --git a/test/unit/storage/accessors/AtomicFileDataAccessor.test.ts b/test/unit/storage/accessors/AtomicFileDataAccessor.test.ts index dab2c2cb99..15be725a81 100644 --- a/test/unit/storage/accessors/AtomicFileDataAccessor.test.ts +++ b/test/unit/storage/accessors/AtomicFileDataAccessor.test.ts @@ -65,7 +65,7 @@ describe('AtomicFileDataAccessor', (): void => { await expect(accessor.writeDocument({ path: `${base}res.ttl` }, data, metadata)).rejects.toThrow('error'); }); - it('should throw when renameing / moving the file goes wrong.', async(): Promise => { + it('should throw when renaming / moving the file goes wrong.', async(): Promise => { jest.requireMock('fs').promises.rename = jest.fn((): any => { throw new Error('error'); }); @@ -84,5 +84,14 @@ describe('AtomicFileDataAccessor', (): void => { })); await expect(accessor.writeDocument({ path: `${base}res.ttl` }, data, metadata)).rejects.toThrow('error'); }); + + it('should throw when renaming / moving the file goes wrong and the temp file does not exist.', + async(): Promise => { + jest.requireMock('fs').promises.rename = jest.fn((): any => { + throw new Error('error'); + }); + jest.requireMock('fs').promises.stat = jest.fn(); + await expect(accessor.writeDocument({ path: `${base}res.ttl` }, data, metadata)).rejects.toThrow('error'); + }); }); });