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:
@@ -207,53 +207,59 @@ function cwdToDashed(cwd: string): string {
|
||||
return cwd.replace(/\//g, '-');
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the last assistant message text from parsed transcript lines.
|
||||
*/
|
||||
function parseAssistantTextFromLine(line: string): string | null {
|
||||
if (!line.includes('"type":"assistant"')) return null;
|
||||
|
||||
const entry = JSON.parse(line);
|
||||
if (entry.type === 'assistant' && entry.message?.content && Array.isArray(entry.message.content)) {
|
||||
let text = '';
|
||||
for (const block of entry.message.content) {
|
||||
if (block.type === 'text') text += block.text;
|
||||
}
|
||||
text = text.replace(SYSTEM_REMINDER_REGEX, '').trim();
|
||||
if (text) return text;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function findLastAssistantMessage(lines: string[]): string {
|
||||
for (let i = lines.length - 1; i >= 0; i--) {
|
||||
try {
|
||||
const result = parseAssistantTextFromLine(lines[i]);
|
||||
if (result) return result;
|
||||
} catch (parseError) {
|
||||
if (parseError instanceof Error) {
|
||||
logger.debug('WORKER', 'Skipping malformed transcript line', { lineIndex: i }, parseError);
|
||||
} else {
|
||||
logger.debug('WORKER', 'Skipping malformed transcript line', { lineIndex: i, error: String(parseError) });
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract prior messages from transcript file
|
||||
*/
|
||||
export function extractPriorMessages(transcriptPath: string): PriorMessages {
|
||||
try {
|
||||
if (!existsSync(transcriptPath)) {
|
||||
return { userMessage: '', assistantMessage: '' };
|
||||
}
|
||||
|
||||
if (!existsSync(transcriptPath)) return { userMessage: '', assistantMessage: '' };
|
||||
const content = readFileSync(transcriptPath, 'utf-8').trim();
|
||||
if (!content) {
|
||||
return { userMessage: '', assistantMessage: '' };
|
||||
}
|
||||
if (!content) return { userMessage: '', assistantMessage: '' };
|
||||
|
||||
const lines = content.split('\n').filter(line => line.trim());
|
||||
let lastAssistantMessage = '';
|
||||
|
||||
for (let i = lines.length - 1; i >= 0; i--) {
|
||||
try {
|
||||
const line = lines[i];
|
||||
if (!line.includes('"type":"assistant"')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const entry = JSON.parse(line);
|
||||
if (entry.type === 'assistant' && entry.message?.content && Array.isArray(entry.message.content)) {
|
||||
let text = '';
|
||||
for (const block of entry.message.content) {
|
||||
if (block.type === 'text') {
|
||||
text += block.text;
|
||||
}
|
||||
}
|
||||
text = text.replace(SYSTEM_REMINDER_REGEX, '').trim();
|
||||
if (text) {
|
||||
lastAssistantMessage = text;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch (parseError) {
|
||||
logger.debug('PARSER', 'Skipping malformed transcript line', { lineIndex: i }, parseError as Error);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
const lastAssistantMessage = findLastAssistantMessage(lines);
|
||||
return { userMessage: '', assistantMessage: lastAssistantMessage };
|
||||
} catch (error) {
|
||||
logger.failure('WORKER', `Failed to extract prior messages from transcript`, { transcriptPath }, error as Error);
|
||||
if (error instanceof Error) {
|
||||
logger.failure('WORKER', 'Failed to extract prior messages from transcript', { transcriptPath }, error);
|
||||
} else {
|
||||
logger.warn('WORKER', 'Failed to extract prior messages from transcript', { transcriptPath, error: String(error) });
|
||||
}
|
||||
return { userMessage: '', assistantMessage: '' };
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user