`readFile` on directory should throw meaningful errors
Closed this issue · 4 comments
zardoy commented
I tested reading file with WebAccess backend which was actually a directory and it gave me the following error:
(in promise) TypeError: Cannot read properties of undefined (reading 'close')
at _readFile (promises.js:471:20)
For example, calling readdir on a file gives the following error: ENOTDIR: File is not a directory
james-pre commented
Thanks I will look into this. Could you include a full stack trace?
Edit: The error is probably because _open
failed without throwing, so file
is undefined.
try {
const stat = await file.stat();
const data = new Uint8Array(stat.size);
await file.read(data, 0, stat.size, 0);
await file.close();
return data;
} catch (e) {
await file.close(); // here?
throw e;
}
james-pre commented
Ah yes,
WebAccessFS.openFile
:
public async openFile(path: string, flag: string): Promise<PreloadFile<this>> {
const handle = await this.getHandle(path);
if (handle instanceof FileSystemFileHandle) {
const file = await handle.getFile();
const data = new Uint8Array(await file.arrayBuffer());
const stats = new Stats({ mode: 0o777 | FileType.FILE, size: file.size, mtimeMs: file.lastModified });
return new PreloadFile(this, path, flag, stats, data);
}
// uh oh, undefined returned. This should throw an error instead.
}
zardoy commented
hhey sorry i didnt update yet to check it. in fact since everything is the same in terms of performance as in browserfs I'm not sure if I'm updating for now