Files
claude-mem/src/services/worker/SettingsManager.ts
T
Alex Newman a0dd516cd5 fix: resolve all 301 error handling anti-patterns across codebase
Systematic cleanup of every error handling anti-pattern detected by the
automated scanner. 289 issues fixed via code changes, 12 approved with
specific technical justifications.

Changes across 90 files:
- GENERIC_CATCH (141): Added instanceof Error type discrimination
- LARGE_TRY_BLOCK (82): Extracted helper methods to narrow try scope to ≤10 lines
- NO_LOGGING_IN_CATCH (65): Added logger/console calls for error visibility
- CATCH_AND_CONTINUE_CRITICAL_PATH (10): Added throw/return or approved overrides
- ERROR_STRING_MATCHING (2): Approved with rationale (no typed error classes)
- ERROR_MESSAGE_GUESSING (1): Replaced chained .includes() with documented pattern array
- PROMISE_CATCH_NO_LOGGING (1): Added logging to .catch() handler

Also fixes a detector bug where nested try/catch inside a catch block
corrupted brace-depth tracking, causing false positives.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-19 19:57:00 -07:00

73 lines
2.0 KiB
TypeScript

/**
* SettingsManager: DRY settings CRUD utility
*
* Responsibility:
* - DRY helper for viewer settings CRUD
* - Eliminates duplication in settings read/write logic
* - Type-safe settings management
*/
import { DatabaseManager } from './DatabaseManager.js';
import { logger } from '../../utils/logger.js';
import type { ViewerSettings } from '../worker-types.js';
export class SettingsManager {
private dbManager: DatabaseManager;
private readonly defaultSettings: ViewerSettings = {
sidebarOpen: true,
selectedProject: null,
theme: 'system'
};
constructor(dbManager: DatabaseManager) {
this.dbManager = dbManager;
}
/**
* Get current viewer settings (with defaults)
*/
getSettings(): ViewerSettings {
const db = this.dbManager.getSessionStore().db;
try {
const stmt = db.prepare('SELECT key, value FROM viewer_settings');
const rows = stmt.all() as Array<{ key: string; value: string }>;
const settings: ViewerSettings = { ...this.defaultSettings };
for (const row of rows) {
const key = row.key as keyof ViewerSettings;
if (key in settings) {
settings[key] = JSON.parse(row.value) as ViewerSettings[typeof key];
}
}
return settings;
} catch (error) {
if (error instanceof Error) {
logger.debug('WORKER', 'Failed to load settings, using defaults', {}, error);
} else {
logger.debug('WORKER', 'Failed to load settings, using defaults', { rawError: String(error) });
}
return { ...this.defaultSettings };
}
}
/**
* Update viewer settings (partial update)
*/
updateSettings(updates: Partial<ViewerSettings>): ViewerSettings {
const db = this.dbManager.getSessionStore().db;
const stmt = db.prepare(`
INSERT OR REPLACE INTO viewer_settings (key, value)
VALUES (?, ?)
`);
for (const [key, value] of Object.entries(updates)) {
stmt.run(key, JSON.stringify(value));
}
return this.getSettings();
}
}