Skip to content

Commit

Permalink
handle Error: ENOENT
Browse files Browse the repository at this point in the history
  • Loading branch information
matt8707 committed Dec 26, 2023
1 parent 8231957 commit ea24fb2
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/routes/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,17 @@ dotenv.config();
async function loadFile(file: string) {
try {
const data = await readFile(file, 'utf8');
return file.endsWith('.yaml') ? yaml.load(data) : JSON.parse(data);
if (!data.trim()) {
return {}; // file is empty, early return object
} else {
return file.endsWith('.yaml') ? yaml.load(data) : JSON.parse(data);
}
} catch (error) {
console.error(`Error reading or parsing ${file}:`, error);
if ((error as NodeJS.ErrnoException)?.code === 'ENOENT') {
console.error(`No existing file found for ${file}`);
} else {
console.error(`Error reading or parsing ${file}:`, error);
}
return {};
}
}
Expand Down

0 comments on commit ea24fb2

Please sign in to comment.