From 13645af300b3a85842a5419b8176fb73107d67d5 Mon Sep 17 00:00:00 2001 From: Alexander Kucera Date: Fri, 24 Oct 2025 16:41:53 +0200 Subject: [PATCH] Improves data loading with robust error handling Adds comprehensive error handling for plugin data loading to prevent potential startup failures Implements graceful fallback to default settings when data loading fails or is corrupted Ensures plugin can initialize even with invalid or missing persistent data Separates model cache extraction to prevent type drift and improve data integrity --- main.ts | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/main.ts b/main.ts index aef3700..06039b2 100644 --- a/main.ts +++ b/main.ts @@ -2,7 +2,7 @@ // ABOUTME: Coordinates between services, commands, and Obsidian's plugin lifecycle import { Editor, MarkdownView, Notice, Plugin } from 'obsidian'; -import { FreewritingPromptsSettings, FreewritingPromptsData } from './types'; +import { FreewritingPromptsSettings, FreewritingPromptsData, ModelCache } from './types'; import { DEFAULT_SETTINGS, FreewritingPromptsSettingTab } from './settings'; import { PromptGeneratorService } from './services/promptGenerator'; import { ModelService } from './services/modelService'; @@ -50,11 +50,25 @@ export default class FreewritingPromptsPlugin extends Plugin { */ async onload() { // Load data once and use it for both settings and model cache - const data = await this.loadData() as FreewritingPromptsData | null; - - // Destructure to prevent modelCache from being merged into settings (type drift) - const { modelCache, ...savedSettings } = data ?? {}; - this.settings = Object.assign({}, DEFAULT_SETTINGS, savedSettings); + // Guard against corrupted persisted data to prevent plugin load failure + let modelCache: ModelCache | undefined; + try { + const data = await this.loadData() as FreewritingPromptsData | null; + if (data) { + // Destructure to prevent modelCache from being merged into settings (type drift) + const { modelCache: cache, ...savedSettings } = data; + modelCache = cache; + this.settings = Object.assign({}, DEFAULT_SETTINGS, savedSettings); + } else { + // No data loaded, use defaults + this.settings = { ...DEFAULT_SETTINGS }; + modelCache = undefined; + } + } catch (error) { + console.error('Failed to load settings; using defaults.', error); + this.settings = { ...DEFAULT_SETTINGS }; + modelCache = undefined; + } // Track initial API key to detect changes on save this.lastApiKey = this.settings.apiKey;