From e48af55635a820701313b612837567d292a75b8c Mon Sep 17 00:00:00 2001 From: Alexander Kucera Date: Wed, 3 Sep 2025 23:09:36 +0200 Subject: [PATCH] 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. --- main.ts | 29 ++++++++++++++++++++++---- src/converters/PandocTypstConverter.ts | 11 +++++++++- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/main.ts b/main.ts index 1873c1c..ea6d0b2 100644 --- a/main.ts +++ b/main.ts @@ -960,7 +960,11 @@ ${dependencyResult.allAvailable // Create output directory if it doesn't exist 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 @@ -971,7 +975,11 @@ ${dependencyResult.allAvailable relativePath = folderPath; const fullOutputDir = path.join(outputDir, relativePath); 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() { - 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() { - 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 + } } /** diff --git a/src/converters/PandocTypstConverter.ts b/src/converters/PandocTypstConverter.ts index f59bb5f..12ae15e 100644 --- a/src/converters/PandocTypstConverter.ts +++ b/src/converters/PandocTypstConverter.ts @@ -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 {