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
This commit is contained in:
AlexKucera committed 2025-10-24 16:41:53 +02:00
1 parent c9adf05a82
commit 13645af300
1 file changed
+17 -3
+17 -3
View File
@@ -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
// 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, ...savedSettings } = data ?? {};
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;