ai-pp3
Version:
CLI tool combining multimodal AI analysis with RawTherapee's engine to generate optimized PP3 profiles for RAW photography
44 lines • 1.82 kB
JavaScript
import fs from "node:fs";
function getAccessMode(mode) {
return mode === "read" ? fs.constants.R_OK : fs.constants.W_OK;
}
function createNotFoundError(mode, filePath) {
const resourceType = mode === "read" ? "File" : "Directory";
return new Error(`${resourceType} not found: ${filePath}`);
}
function createPermissionError(mode, filePath) {
const operation = mode === "read" ? "reading" : "writing";
return new Error(`Permission denied ${operation} ${filePath}`);
}
function createGenericError(filePath, error) {
const message = error instanceof Error ? error.message : "Unknown error";
return new Error(`Error accessing ${filePath}: ${message}`);
}
export async function validateFileAccess(filePath, mode) {
try {
await fs.promises.access(filePath, getAccessMode(mode));
}
catch (error) {
if (error instanceof Error && "code" in error) {
if (error.code === "ENOENT") {
throw createNotFoundError(mode, filePath);
}
else if (error.code === "EACCES") {
throw createPermissionError(mode, filePath);
}
}
throw createGenericError(filePath, error);
}
}
export function handleFileError(error, filePath, operation) {
if (error instanceof Error && "code" in error) {
if (error.code === "ENOENT") {
throw new Error(`File not found during ${operation}: ${filePath}`);
}
else if (error.code === "EACCES") {
throw new Error(`Permission denied ${operation === "read" ? "reading" : "writing"} file: ${filePath}`);
}
}
throw new Error(`Error ${operation === "read" ? "reading" : "writing"} file ${filePath}: ${error instanceof Error ? error.message : "Unknown error"}`);
}
//# sourceMappingURL=validation.js.map