Add FileNotFoundError error

This commit is contained in:
Andras Schmelczer 2025-02-23 10:04:20 +00:00
parent 5bd92c8412
commit 0612f15aad
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
2 changed files with 96 additions and 3 deletions

View file

@ -2,12 +2,17 @@ import type { Logger } from "src/tracing/logger";
import type { FileSystemOperations } from "./filesystem-operations";
import type { RelativePath } from "src/persistence/database";
import { isBinary, isFileTypeMergable, mergeText } from "sync_lib";
import { SafeFileSystemOperations } from "./safe-filesystem-operations";
export class FileOperations {
private readonly fs: SafeFileSystemOperations;
public constructor(
private readonly logger: Logger,
private readonly fs: FileSystemOperations
) {}
fs: FileSystemOperations
) {
this.fs = new SafeFileSystemOperations(fs);
}
public async listAllFiles(): Promise<RelativePath[]> {
const files = await this.fs.listAllFiles();
@ -43,7 +48,7 @@ export class FileOperations {
}
public async exists(path: RelativePath): Promise<boolean> {
this.logger.debug(`Checking existance of ${path}`);
this.logger.debug(`Checking existence of ${path}`);
return this.fs.exists(path);
}

View file

@ -0,0 +1,88 @@
import { FileSystemOperations } from "dist/types";
import type { RelativePath } from "src/persistence/database";
export class FileNotFoundError extends Error {
constructor(message: string) {
super(message);
this.name = "FileNotFoundError";
}
}
// Decorate FileSystemOperations replacing errors with FileNotFoundError
// if the accessed file doesn't exist.
export class SafeFileSystemOperations implements FileSystemOperations {
public constructor(private readonly fs: FileSystemOperations) {}
public listAllFiles(): Promise<RelativePath[]> {
return this.fs.listAllFiles();
}
public async read(path: RelativePath): Promise<Uint8Array> {
return this.safeOperation(path, () => this.fs.read(path));
}
public async write(path: RelativePath, content: Uint8Array): Promise<void> {
return this.fs.write(path, content);
}
public async atomicUpdateText(
path: RelativePath,
updater: (currentContent: string) => string
): Promise<string> {
return this.safeOperation(path, () =>
this.fs.atomicUpdateText(path, updater)
);
}
public async getFileSize(path: RelativePath): Promise<number> {
return this.safeOperation(path, () => this.fs.getFileSize(path));
}
public async getModificationTime(path: RelativePath): Promise<Date> {
return this.safeOperation(path, () =>
this.fs.getModificationTime(path)
);
}
public async exists(path: RelativePath): Promise<boolean> {
return this.fs.exists(path);
}
public async createDirectory(path: RelativePath): Promise<void> {
return this.fs.createDirectory(path);
}
public async delete(path: RelativePath): Promise<void> {
return this.fs.delete(path);
}
public async rename(
oldPath: RelativePath,
newPath: RelativePath
): Promise<void> {
return this.safeOperation(oldPath, () =>
this.fs.rename(oldPath, newPath)
);
}
private async safeOperation<T>(
path: RelativePath,
operation: () => Promise<T>
): Promise<T> {
// Without locking the file, this isn't atomic, however, it's good enough practicaly.
// This will only break if the file exists, gets deleted and then immediately
// recreated while `operation` is running.
if (!(await this.fs.exists(path))) {
throw new FileNotFoundError(path);
}
try {
return await operation();
} catch (error) {
if (await this.fs.exists(path)) {
throw error;
} else {
throw new FileNotFoundError(path);
}
}
}
}