Skip to content

Commit

Permalink
fix: added extra try catch for breaking bug
Browse files Browse the repository at this point in the history
  • Loading branch information
BelgianNoise committed Jan 14, 2022
1 parent b16a63d commit 0d526ea
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/storage/accessors/AtomicFileDataAccessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
11 changes: 10 additions & 1 deletion test/unit/storage/accessors/AtomicFileDataAccessor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> => {
it('should throw when renaming / moving the file goes wrong.', async(): Promise<void> => {
jest.requireMock('fs').promises.rename = jest.fn((): any => {
throw new Error('error');
});
Expand All @@ -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<void> => {
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');
});
});
});

0 comments on commit 0d526ea

Please sign in to comment.