Skip to content

Commit

Permalink
fix: 🐛 throw "ENOENT" and "ENOTDIR" when folder or file 404
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jun 20, 2023
1 parent cd2591a commit 5de4faa
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/fsa-to-node/FsaNodeCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,14 @@ export class FsaNodeCore {
curr = await curr.getDirectoryHandle(name, options);
}
} catch (error) {
if (error && typeof error === 'object' && error.name === 'TypeMismatchError')
throw createError('ENOTDIR', funcName, path.join(FsaToNodeConstants.Separator));
if (error && typeof error === 'object') {
switch (error.name) {
case 'TypeMismatchError':
throw createError('ENOTDIR', funcName, path.join(FsaToNodeConstants.Separator));
case 'NotFoundError':
throw createError('ENOENT', funcName, path.join(FsaToNodeConstants.Separator));
}
}
throw error;
}
return curr;
Expand Down Expand Up @@ -86,7 +92,18 @@ export class FsaNodeCore {
if (error && typeof error === 'object') {
switch (error.name) {
case 'TypeMismatchError':
return await dir.getDirectoryHandle(name);
try {
return await dir.getDirectoryHandle(name);
} catch (error2) {
if (error2 && typeof error2 === 'object') {
switch (error2.name) {
case 'TypeMismatchError':
throw createError('ENOTDIR', funcName, path.join(FsaToNodeConstants.Separator));
case 'NotFoundError':
throw createError('ENOENT', funcName, path.join(FsaToNodeConstants.Separator));
}
}
}
case 'NotFoundError':
throw createError('ENOENT', funcName, path.join(FsaToNodeConstants.Separator));
}
Expand Down
12 changes: 12 additions & 0 deletions src/fsa-to-node/__tests__/FsaNodeFs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,18 @@ onlyOnNode20('FsaNodeFs', () => {
expect(stats.isFile()).toBe(true);
});

test('throws "ENOENT" when path is not found', async () => {
const { fs } = setup({ folder: { file: 'test' }, 'empty-folder': null, 'f.html': 'test' });
const [, error] = await of(fs.promises.stat('/folder/repo/.git'));
expect((<any>error).code).toBe('ENOENT');
});

test('throws "ENOTDIR" when sub-folder is a file', async () => {
const { fs } = setup({ folder: { file: 'test' }, 'empty-folder': null, 'f.html': 'test' });
const [, error] = await of(fs.promises.stat('/folder/file/repo/.git'));
expect((<any>error).code).toBe('ENOTDIR');
});

test('can retrieve file size', async () => {
const { fs, mfs } = setup({ folder: { file: 'test' }, 'empty-folder': null, 'f.html': 'test' });
const stats = await fs.promises.stat('/folder/file');
Expand Down

0 comments on commit 5de4faa

Please sign in to comment.