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>
This commit is contained in:
Alex Newman
2026-04-19 19:57:00 -07:00
parent c9adb1c77b
commit a0dd516cd5
91 changed files with 4846 additions and 3414 deletions
+26 -21
View File
@@ -105,17 +105,13 @@ async function workerPost(
path: string,
body: Record<string, unknown>,
): Promise<Record<string, unknown> | null> {
let response: Response;
try {
const response = await fetch(`${WORKER_BASE_URL}${path}`, {
response = await fetch(`${WORKER_BASE_URL}${path}`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
if (!response.ok) {
console.warn(`[claude-mem] Worker POST ${path} returned ${response.status}`);
return null;
}
return (await response.json()) as Record<string, unknown>;
} catch (error: unknown) {
// Gracefully handle ECONNREFUSED — worker may not be running
const message = error instanceof Error ? error.message : String(error);
@@ -124,6 +120,12 @@ async function workerPost(
}
return null;
}
if (!response.ok) {
console.warn(`[claude-mem] Worker POST ${path} returned ${response.status}`);
return null;
}
return (await response.json()) as Record<string, unknown>;
}
function workerPostFireAndForget(
@@ -339,24 +341,27 @@ export const ClaudeMemPlugin = async (ctx: OpenCodePluginContext) => {
return "claude-mem worker is not running. Start it with: npx claude-mem start";
}
let data: any;
try {
const data = JSON.parse(text);
const items = Array.isArray(data.items) ? data.items : [];
if (items.length === 0) {
return `No results found for "${query}".`;
}
return items
.slice(0, 10)
.map((item: Record<string, unknown>, index: number) => {
const title = String(item.title || item.subtitle || "Untitled");
const project = item.project ? ` [${String(item.project)}]` : "";
return `${index + 1}. ${title}${project}`;
})
.join("\n");
} catch {
data = JSON.parse(text);
} catch (error: unknown) {
console.warn('[claude-mem] Failed to parse search results:', error instanceof Error ? error.message : String(error));
return "Failed to parse search results.";
}
const items = Array.isArray(data.items) ? data.items : [];
if (items.length === 0) {
return `No results found for "${query}".`;
}
return items
.slice(0, 10)
.map((item: Record<string, unknown>, index: number) => {
const title = String(item.title || item.subtitle || "Untitled");
const project = item.project ? ` [${String(item.project)}]` : "";
return `${index + 1}. ${title}${project}`;
})
.join("\n");
},
} satisfies ToolDefinition,
},