Skip to content

Commit b0cf11b

Browse files
echobtfactorydroid
andauthored
fix(settings): add fault tolerance and robust defaults for corrupted config files (#346)
- Remove unused comment in ThemeCustomizer.tsx - Fix Theme import in ThemePreview.tsx (use type-only import) - Enhance loadSettings in SettingsContext.tsx: - Validate loaded settings have required structure - Deep merge with DEFAULT_SETTINGS for missing nested sections - Fall back to default settings when loading fails (e.g., corrupted config) - Add console error logging for debugging These changes ensure the GUI remains functional even when config files are corrupted or missing, preventing 'Cannot read properties of undefined' errors. Co-authored-by: Droid Agent <droid@factory.ai>
1 parent 2077cd5 commit b0cf11b

3 files changed

Lines changed: 26 additions & 3 deletions

File tree

cortex-gui/src/components/settings/ThemeCustomizer.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
TERMINAL_COLOR_TOKENS,
1010
} from "@/context/ThemeContext";
1111
import { SectionHeader, Button } from "./FormComponents";
12-
// tokens imported for future use
1312

1413
// ============================================================================
1514
// Types

cortex-gui/src/components/settings/ThemePreview.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createSignal, createMemo, onCleanup, For, Show, batch } from "solid-js";
2+
import type { Theme } from "@/context/ThemeContext";
23
import {
34
useTheme,
4-
Theme,
55
DEFAULT_DARK_COLORS,
66
DEFAULT_LIGHT_COLORS,
77
} from "@/context/ThemeContext";

cortex-gui/src/context/SettingsContext.tsx

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1144,15 +1144,39 @@ export function SettingsProvider(props: ParentProps) {
11441144
});
11451145
try {
11461146
const loaded = await invoke<CortexSettings>("settings_load");
1147+
// Validate loaded settings have required structure, fallback to defaults for missing sections
1148+
const validated: CortexSettings = {
1149+
...DEFAULT_SETTINGS,
1150+
...loaded,
1151+
// Ensure nested objects have defaults merged properly
1152+
editor: { ...DEFAULT_SETTINGS.editor, ...loaded?.editor },
1153+
theme: { ...DEFAULT_SETTINGS.theme, ...loaded?.theme },
1154+
terminal: { ...DEFAULT_SETTINGS.terminal, ...loaded?.terminal },
1155+
ai: { ...DEFAULT_SETTINGS.ai, ...loaded?.ai },
1156+
security: { ...DEFAULT_SETTINGS.security, ...loaded?.security },
1157+
files: { ...DEFAULT_SETTINGS.files, ...loaded?.files },
1158+
explorer: { ...DEFAULT_SETTINGS.explorer, ...loaded?.explorer },
1159+
zenMode: { ...DEFAULT_SETTINGS.zenMode, ...loaded?.zenMode },
1160+
screencastMode: { ...DEFAULT_SETTINGS.screencastMode, ...loaded?.screencastMode },
1161+
search: { ...DEFAULT_SETTINGS.search, ...loaded?.search },
1162+
debug: { ...DEFAULT_SETTINGS.debug, ...loaded?.debug },
1163+
git: { ...DEFAULT_SETTINGS.git, ...loaded?.git },
1164+
http: { ...DEFAULT_SETTINGS.http, ...loaded?.http },
1165+
commandPalette: { ...DEFAULT_SETTINGS.commandPalette, ...loaded?.commandPalette },
1166+
workbench: { ...DEFAULT_SETTINGS.workbench, ...loaded?.workbench, editor: { ...DEFAULT_SETTINGS.workbench.editor, ...loaded?.workbench?.editor } },
1167+
};
11471168
batch(() => {
1148-
setWsState("userSettings", reconcile(loaded));
1169+
setWsState("userSettings", reconcile(validated));
11491170
setState("isDirty", false);
11501171
});
11511172
} catch (e) {
11521173
const msg = e instanceof Error ? e.message : String(e);
1174+
console.error("[SettingsContext] Failed to load settings, using defaults:", msg);
11531175
batch(() => {
11541176
setState("error", msg);
11551177
setWsState("error", msg);
1178+
// Fall back to default settings when loading fails (e.g., corrupted config file)
1179+
setWsState("userSettings", reconcile({ ...DEFAULT_SETTINGS }));
11561180
});
11571181
} finally {
11581182
batch(() => {

0 commit comments

Comments
 (0)