Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

readFile on directory should throw meaningful errors #11

Open
zardoy opened this issue May 6, 2024 · 3 comments
Open

readFile on directory should throw meaningful errors #11

zardoy opened this issue May 6, 2024 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@zardoy
Copy link

zardoy commented May 6, 2024

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

@dr-vortex dr-vortex changed the title readFile operations on directory should throw meaningful errors readFile on directory should throw meaningful errors May 6, 2024
@dr-vortex dr-vortex self-assigned this May 6, 2024
@dr-vortex dr-vortex added the enhancement New feature or request label May 6, 2024
@dr-vortex
Copy link
Member

dr-vortex commented May 6, 2024

@zardoy,

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;
}

@dr-vortex
Copy link
Member

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.
}

@dr-vortex dr-vortex transferred this issue from zen-fs/core May 6, 2024
@dr-vortex
Copy link
Member

@zardoy v0.2.5 should fix the issue.

@dr-vortex dr-vortex added bug Something isn't working and removed enhancement New feature or request labels May 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants