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:
@@ -15,6 +15,7 @@ import { writeFileSync, readFileSync, mkdtempSync, rmSync, existsSync } from "no
|
||||
import { join, dirname } from "node:path";
|
||||
import { tmpdir } from "node:os";
|
||||
import { createRequire } from "node:module";
|
||||
import { logger } from "../../utils/logger.js";
|
||||
|
||||
// CJS-safe require for resolving external packages at runtime.
|
||||
// In ESM: import.meta.url works. In CJS bundle (esbuild): __filename works.
|
||||
@@ -160,6 +161,7 @@ export function loadUserGrammars(projectRoot: string): UserGrammarConfig {
|
||||
const content = readFileSync(configPath, "utf-8");
|
||||
rawConfig = JSON.parse(content);
|
||||
} catch {
|
||||
// [ANTI-PATTERN IGNORED]: .claude-mem.json missing is the normal case for most projects
|
||||
userGrammarCache.set(projectRoot, EMPTY_USER_GRAMMAR_CONFIG);
|
||||
return EMPTY_USER_GRAMMAR_CONFIG;
|
||||
}
|
||||
@@ -274,7 +276,9 @@ function resolveGrammarPath(language: string): string | null {
|
||||
const rootPkgPath = _require.resolve(pkg + "/package.json");
|
||||
const resolved = join(dirname(rootPkgPath), subdir);
|
||||
if (existsSync(join(resolved, "src"))) return resolved;
|
||||
} catch { /* fall through */ }
|
||||
} catch {
|
||||
// [ANTI-PATTERN IGNORED]: grammar package not installed is expected for unsupported languages
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -282,6 +286,7 @@ function resolveGrammarPath(language: string): string | null {
|
||||
const packageJsonPath = _require.resolve(pkg + "/package.json");
|
||||
return dirname(packageJsonPath);
|
||||
} catch {
|
||||
// [ANTI-PATTERN IGNORED]: grammar package not installed is expected for unsupported languages
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -550,7 +555,9 @@ function getTreeSitterBin(): string {
|
||||
cachedBinPath = binPath;
|
||||
return binPath;
|
||||
}
|
||||
} catch { /* fall through */ }
|
||||
} catch {
|
||||
// [ANTI-PATTERN IGNORED]: tree-sitter-cli not in node_modules is expected; falls back to PATH
|
||||
}
|
||||
|
||||
// Fallback: assume it's on PATH
|
||||
cachedBinPath = "tree-sitter";
|
||||
@@ -585,7 +592,8 @@ function runBatchQuery(queryFile: string, sourceFiles: string[], grammarPath: st
|
||||
let output: string;
|
||||
try {
|
||||
output = execFileSync(bin, execArgs, { encoding: "utf-8", timeout: 30000, stdio: ["pipe", "pipe", "pipe"] });
|
||||
} catch {
|
||||
} catch (error) {
|
||||
logger.debug('WORKER', `tree-sitter query failed for ${sourceFiles.length} file(s)`, undefined, error instanceof Error ? error : undefined);
|
||||
return new Map();
|
||||
}
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
import { readFile, readdir, stat } from "node:fs/promises";
|
||||
import { join, relative } from "node:path";
|
||||
import { parseFilesBatch, formatFoldedView, loadUserGrammars, type FoldedFile } from "./parser.js";
|
||||
import { logger } from "../../utils/logger.js";
|
||||
|
||||
const CODE_EXTENSIONS = new Set([
|
||||
".js", ".jsx", ".ts", ".tsx", ".mjs", ".cjs",
|
||||
@@ -78,7 +79,8 @@ async function* walkDir(dir: string, rootDir: string, maxDepth: number = 20, ext
|
||||
let entries;
|
||||
try {
|
||||
entries = await readdir(dir, { withFileTypes: true });
|
||||
} catch {
|
||||
} catch (error) {
|
||||
logger.debug('WORKER', `walkDir: failed to read directory ${dir}`, undefined, error instanceof Error ? error : undefined);
|
||||
return; // permission denied, etc.
|
||||
}
|
||||
|
||||
@@ -114,7 +116,8 @@ async function safeReadFile(filePath: string): Promise<string | null> {
|
||||
if (content.slice(0, 1000).includes("\0")) return null;
|
||||
|
||||
return content;
|
||||
} catch {
|
||||
} catch (error) {
|
||||
logger.debug('WORKER', `safeReadFile: failed to read ${filePath}`, undefined, error instanceof Error ? error : undefined);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user