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
+35 -5

No files matched your search

+25 -4
View File
@@ -960,7 +960,11 @@ ${dependencyResult.allAvailable
// Create output directory if it doesn't exist // Create output directory if it doesn't exist
if (!fs.existsSync(outputDir)) { if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true }); try {
fs.mkdirSync(outputDir, { recursive: true });
} catch (error) {
throw new Error(`Failed to create output directory ${outputDir}: ${error.message}`);
}
} }
// Preserve folder structure if configured // Preserve folder structure if configured
@@ -971,7 +975,11 @@ ${dependencyResult.allAvailable
relativePath = folderPath; relativePath = folderPath;
const fullOutputDir = path.join(outputDir, relativePath); const fullOutputDir = path.join(outputDir, relativePath);
if (!fs.existsSync(fullOutputDir)) { if (!fs.existsSync(fullOutputDir)) {
fs.mkdirSync(fullOutputDir, { recursive: true }); 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() { 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() { async saveSettings() {
await this.saveData(this.settings); 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); const vaultContents = fs.readdirSync(this.pandocOptions.vaultBasePath);
for (const item of vaultContents) { for (const item of vaultContents) {
const itemPath = path.join(this.pandocOptions.vaultBasePath, item); 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('_')) { if (stat.isDirectory() && !item.startsWith('.') && !item.startsWith('_')) {
// Check if this directory contains images // Check if this directory contains images
try { try {