Adds robustness to file system operations

Improves error handling when creating directories,
loading, and saving settings. This prevents crashes and
provides more informative error messages to the user.

Adds a try-catch block to `fs.statSync` to handle cases where a
file might be deleted between `readdirSync` and `statSync`,
preventing program termination.
This commit is contained in:
AlexKucera committed 2025-09-03 23:09:36 +02:00
1 parent 742b9a14c8
commit e48af55635
2 files changed
+32 -2

No files matched your search

+22 -1
View File
@@ -960,7 +960,11 @@ ${dependencyResult.allAvailable
// Create output directory if it doesn't exist
if (!fs.existsSync(outputDir)) {
try {
fs.mkdirSync(outputDir, { recursive: true });
} catch (error) {
throw new Error(`Failed to create output directory ${outputDir}: ${error.message}`);
}
}
// Preserve folder structure if configured
@@ -971,7 +975,11 @@ ${dependencyResult.allAvailable
relativePath = folderPath;
const fullOutputDir = path.join(outputDir, relativePath);
if (!fs.existsSync(fullOutputDir)) {
try {
fs.mkdirSync(fullOutputDir, { recursive: true });
} catch (error) {
throw new Error(`Failed to create nested output directory ${fullOutputDir}: ${error.message}`);
}
}
}
}
@@ -992,11 +1000,24 @@ ${dependencyResult.allAvailable
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
try {
const data = await this.loadData();
this.settings = Object.assign({}, DEFAULT_SETTINGS, data);
} catch (error) {
console.warn('Failed to load plugin settings, using defaults:', error);
this.settings = Object.assign({}, DEFAULT_SETTINGS);
// Don't show notice for this - it's normal on first load
}
}
async saveSettings() {
try {
await this.saveData(this.settings);
} catch (error) {
console.error('Failed to save plugin settings:', error);
new Notice(`Failed to save settings: ${error.message}`, 4000);
throw error; // Re-throw to let callers handle appropriately
}
}
/**
+10 -1
View File
@@ -305,7 +305,16 @@ export class PandocTypstConverter {
const vaultContents = fs.readdirSync(this.pandocOptions.vaultBasePath);
for (const item of vaultContents) {
const itemPath = path.join(this.pandocOptions.vaultBasePath, item);
const stat = fs.statSync(itemPath);
let stat;
try {
stat = fs.statSync(itemPath);
} catch (error) {
// File might have been deleted between readdirSync and statSync
console.warn(`Export: Unable to stat ${itemPath}:`, error.message);
continue;
}
if (stat.isDirectory() && !item.startsWith('.') && !item.startsWith('_')) {
// Check if this directory contains images
try {