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
+31 -21
View File
@@ -208,31 +208,27 @@ export class Server {
return res.status(400).json({ error: 'Invalid topic' });
}
try {
let content: string;
if (operation && !ALLOWED_OPERATIONS.includes(operation)) {
return res.status(400).json({ error: 'Invalid operation' });
}
if (operation) {
// Validate operation
if (!ALLOWED_OPERATIONS.includes(operation)) {
return res.status(400).json({ error: 'Invalid operation' });
}
// Path boundary check
const OPERATIONS_BASE_DIR = path.resolve(__dirname, '../skills/mem-search/operations');
const operationPath = path.resolve(OPERATIONS_BASE_DIR, `${operation}.md`);
if (!operationPath.startsWith(OPERATIONS_BASE_DIR + path.sep)) {
return res.status(400).json({ error: 'Invalid request' });
}
content = await fs.promises.readFile(operationPath, 'utf-8');
} else {
const skillPath = path.join(__dirname, '../skills/mem-search/SKILL.md');
const fullContent = await fs.promises.readFile(skillPath, 'utf-8');
content = this.extractInstructionSection(fullContent, topic);
if (operation) {
const OPERATIONS_BASE_DIR = path.resolve(__dirname, '../skills/mem-search/operations');
const operationPath = path.resolve(OPERATIONS_BASE_DIR, `${operation}.md`);
if (!operationPath.startsWith(OPERATIONS_BASE_DIR + path.sep)) {
return res.status(400).json({ error: 'Invalid request' });
}
}
res.json({
content: [{ type: 'text', text: content }]
});
try {
const content = await this.loadInstructionContent(operation, topic);
res.json({ content: [{ type: 'text', text: content }] });
} catch (error) {
if (error instanceof Error) {
logger.debug('HTTP', 'Instruction file not found', { topic, operation, message: error.message });
} else {
logger.debug('HTTP', 'Instruction file not found', { topic, operation, error: String(error) });
}
res.status(404).json({ error: 'Instruction not found' });
}
});
@@ -334,6 +330,20 @@ export class Server {
});
}
/**
* Load instruction content from disk for the /api/instructions endpoint.
* Caller must validate operation/topic before calling.
*/
private async loadInstructionContent(operation: string | undefined, topic: string): Promise<string> {
if (operation) {
const operationPath = path.resolve(__dirname, '../skills/mem-search/operations', `${operation}.md`);
return fs.promises.readFile(operationPath, 'utf-8');
}
const skillPath = path.join(__dirname, '../skills/mem-search/SKILL.md');
const fullContent = await fs.promises.readFile(skillPath, 'utf-8');
return this.extractInstructionSection(fullContent, topic);
}
/**
* Extract a specific section from instruction content
*/