feat: Improve theme not found handling

Modify  to return a boolean instead of throwing an error when a theme is not found. Update CLI startup and  hook to handle the boolean return value for more graceful error handling.
This commit is contained in:
Taylor Mullen 2025-05-09 10:20:08 -07:00 committed by N. Taylor Mullen
parent c58f879026
commit b8fa38a6e8
3 changed files with 23 additions and 59 deletions

View File

@ -24,23 +24,10 @@ async function main() {
const settings = loadSettings(process.cwd()); const settings = loadSettings(process.cwd());
const config = await loadCliConfig(settings.merged); const config = await loadCliConfig(settings.merged);
if (settings.merged.theme) { if (settings.merged.theme) {
try { if (!themeManager.setActiveTheme(settings.merged.theme)) {
themeManager.setActiveTheme(settings.merged.theme);
} catch (error: unknown) {
// If the theme is not found during initial load, log a warning and continue. // If the theme is not found during initial load, log a warning and continue.
// The useThemeCommand hook in App.tsx will handle opening the dialog. // The useThemeCommand hook in App.tsx will handle opening the dialog.
if ( console.warn(`Warning: Theme "${settings.merged.theme}" not found.`);
error instanceof Error &&
error.message.includes('Theme') &&
error.message.includes('not found')
) {
console.warn(
`Warning: ${error instanceof Error ? error.message : String(error)}`,
);
} else {
// Re-throw other errors to be caught by the main catch block
throw error;
}
} }
} }

View File

@ -32,28 +32,12 @@ export const useThemeCommand = (
// Apply initial theme on component mount // Apply initial theme on component mount
useEffect(() => { useEffect(() => {
try { if (!themeManager.setActiveTheme(effectiveTheme)) {
themeManager.setActiveTheme(effectiveTheme);
setThemeError(null); // Clear any previous theme error on success
} catch (error: unknown) {
// If theme is not found during initial load, open the theme selection dialog and set error message // If theme is not found during initial load, open the theme selection dialog and set error message
if ( setIsThemeDialogOpen(true);
error instanceof Error && setThemeError(`Theme "${effectiveTheme}" not found.`);
error.message.includes('Theme') && } else {
error.message.includes('not found') setThemeError(null); // Clear any previous theme error on success
) {
setIsThemeDialogOpen(true);
setThemeError(
`Error: ${error instanceof Error ? error.message : String(error)}`,
);
} else {
console.error(
`Error setting initial theme: ${error instanceof Error ? error.message : String(error)}`,
);
setThemeError(
`Error setting initial theme: ${error instanceof Error ? error.message : String(error)}`,
);
}
} }
}, [effectiveTheme, setThemeError]); // Re-run if effectiveTheme or setThemeError changes }, [effectiveTheme, setThemeError]); // Re-run if effectiveTheme or setThemeError changes
@ -63,29 +47,13 @@ export const useThemeCommand = (
const applyTheme = useCallback( const applyTheme = useCallback(
(themeName: string | undefined) => { (themeName: string | undefined) => {
try { if (!themeManager.setActiveTheme(themeName)) {
themeManager.setActiveTheme(themeName); // If theme is not found, open the theme selection dialog and set error message
setIsThemeDialogOpen(true);
setThemeError(`Theme "${themeName}" not found.`);
} else {
setForceRender((v) => v + 1); // Trigger potential re-render setForceRender((v) => v + 1); // Trigger potential re-render
setThemeError(null); // Clear any previous theme error on success setThemeError(null); // Clear any previous theme error on success
} catch (error: unknown) {
// If theme is not found, open the theme selection dialog and set error message
if (
error instanceof Error &&
error.message.includes('Theme') &&
error.message.includes('not found')
) {
setIsThemeDialogOpen(true);
setThemeError(
`Error: ${error instanceof Error ? error.message : String(error)}`,
);
} else {
console.error(
`Error setting theme: ${error instanceof Error ? error.message : String(error)}`,
);
setThemeError(
`Error setting theme: ${error instanceof Error ? error.message : String(error)}`,
);
}
} }
}, },
[setForceRender, setThemeError], [setForceRender, setThemeError],

View File

@ -73,14 +73,23 @@ class ThemeManager {
/** /**
* Sets the active theme. * Sets the active theme.
* @param themeName The name of the theme to activate. * @param themeName The name of the theme to activate.
* @returns True if the theme was successfully set, false otherwise.
*/ */
setActiveTheme(themeName: string | undefined): void { setActiveTheme(themeName: string | undefined): boolean {
const foundTheme = this.findThemeByName(themeName); const foundTheme = this.findThemeByName(themeName);
if (foundTheme) { if (foundTheme) {
this.activeTheme = foundTheme; this.activeTheme = foundTheme;
return true;
} else { } else {
throw new Error(`Theme "${themeName}" not found.`); // If themeName is undefined, it means we want to set the default theme.
// If findThemeByName returns undefined (e.g. default theme is also not found for some reason)
// then this will return false.
if (themeName === undefined) {
this.activeTheme = DEFAULT_THEME;
return true;
}
return false;
} }
} }