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:
1 file changed
+17
-3
@@ -2,7 +2,7 @@
|
|||||||
// ABOUTME: Coordinates between services, commands, and Obsidian's plugin lifecycle
|
// ABOUTME: Coordinates between services, commands, and Obsidian's plugin lifecycle
|
||||||
|
|
||||||
import { Editor, MarkdownView, Notice, Plugin } from 'obsidian';
|
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 { DEFAULT_SETTINGS, FreewritingPromptsSettingTab } from './settings';
|
||||||
import { PromptGeneratorService } from './services/promptGenerator';
|
import { PromptGeneratorService } from './services/promptGenerator';
|
||||||
import { ModelService } from './services/modelService';
|
import { ModelService } from './services/modelService';
|
||||||
@@ -50,11 +50,25 @@ export default class FreewritingPromptsPlugin extends Plugin {
|
|||||||
*/
|
*/
|
||||||
async onload() {
|
async onload() {
|
||||||
// Load data once and use it for both settings and model cache
|
// 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;
|
const data = await this.loadData() as FreewritingPromptsData | null;
|
||||||
|
if (data) {
|
||||||
// Destructure to prevent modelCache from being merged into settings (type drift)
|
// 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);
|
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
|
// Track initial API key to detect changes on save
|
||||||
this.lastApiKey = this.settings.apiKey;
|
this.lastApiKey = this.settings.apiKey;
|
||||||
|
|||||||
Reference in new issue
Block a user