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:
+21
-6
@@ -69,12 +69,20 @@ class Supervisor {
|
||||
} else {
|
||||
await this.stop();
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error('SYSTEM', 'Error during shutdown', {}, error as Error);
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof Error) {
|
||||
logger.error('SYSTEM', 'Error during shutdown', {}, error);
|
||||
} else {
|
||||
logger.error('SYSTEM', 'Error during shutdown (non-Error)', { error: String(error) });
|
||||
}
|
||||
try {
|
||||
await this.stop();
|
||||
} catch (stopError) {
|
||||
logger.debug('SYSTEM', 'Supervisor shutdown fallback failed', {}, stopError as Error);
|
||||
} catch (stopError: unknown) {
|
||||
if (stopError instanceof Error) {
|
||||
logger.debug('SYSTEM', 'Supervisor shutdown fallback failed', {}, stopError);
|
||||
} else {
|
||||
logger.debug('SYSTEM', 'Supervisor shutdown fallback failed', { error: String(stopError) });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -161,8 +169,15 @@ export function validateWorkerPidFile(options: ValidateWorkerPidOptions = {}): V
|
||||
|
||||
try {
|
||||
pidInfo = JSON.parse(readFileSync(pidFilePath, 'utf-8')) as PidInfo;
|
||||
} catch (error) {
|
||||
logger.warn('SYSTEM', 'Failed to parse worker PID file, removing it', { path: pidFilePath }, error as Error);
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof Error) {
|
||||
logger.warn('SYSTEM', 'Failed to parse worker PID file, removing it', { path: pidFilePath }, error);
|
||||
} else {
|
||||
logger.warn('SYSTEM', 'Failed to parse worker PID file, removing it', {
|
||||
path: pidFilePath,
|
||||
error: String(error)
|
||||
});
|
||||
}
|
||||
rmSync(pidFilePath, { force: true });
|
||||
return 'invalid';
|
||||
}
|
||||
|
||||
@@ -33,8 +33,14 @@ export function isPidAlive(pid: number): boolean {
|
||||
process.kill(pid, 0);
|
||||
return true;
|
||||
} catch (error: unknown) {
|
||||
const code = (error as NodeJS.ErrnoException).code;
|
||||
return code === 'EPERM';
|
||||
if (error instanceof Error) {
|
||||
const code = (error as NodeJS.ErrnoException).code;
|
||||
if (code === 'EPERM') return true;
|
||||
logger.debug('SYSTEM', 'PID check failed', { pid, code });
|
||||
return false;
|
||||
}
|
||||
logger.warn('SYSTEM', 'PID check threw non-Error', { pid, error: String(error) });
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,10 +71,17 @@ export class ProcessRegistry {
|
||||
for (const [id, info] of Object.entries(processes)) {
|
||||
this.entries.set(id, info);
|
||||
}
|
||||
} catch (error) {
|
||||
logger.warn('SYSTEM', 'Failed to parse supervisor registry, rebuilding', {
|
||||
path: this.registryPath
|
||||
}, error as Error);
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof Error) {
|
||||
logger.warn('SYSTEM', 'Failed to parse supervisor registry, rebuilding', {
|
||||
path: this.registryPath
|
||||
}, error);
|
||||
} else {
|
||||
logger.warn('SYSTEM', 'Failed to parse supervisor registry, rebuilding', {
|
||||
path: this.registryPath,
|
||||
error: String(error)
|
||||
});
|
||||
}
|
||||
this.entries.clear();
|
||||
}
|
||||
|
||||
@@ -168,11 +181,18 @@ export class ProcessRegistry {
|
||||
try {
|
||||
process.kill(record.pid, 'SIGTERM');
|
||||
} catch (error: unknown) {
|
||||
const code = (error as NodeJS.ErrnoException).code;
|
||||
if (code !== 'ESRCH') {
|
||||
logger.debug('SYSTEM', `Failed to SIGTERM session process PID ${record.pid}`, {
|
||||
pid: record.pid
|
||||
}, error as Error);
|
||||
if (error instanceof Error) {
|
||||
const code = (error as NodeJS.ErrnoException).code;
|
||||
if (code !== 'ESRCH') {
|
||||
logger.debug('SYSTEM', `Failed to SIGTERM session process PID ${record.pid}`, {
|
||||
pid: record.pid
|
||||
}, error);
|
||||
}
|
||||
} else {
|
||||
logger.warn('SYSTEM', `Failed to SIGTERM session process PID ${record.pid} (non-Error)`, {
|
||||
pid: record.pid,
|
||||
error: String(error)
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -195,11 +215,18 @@ export class ProcessRegistry {
|
||||
try {
|
||||
process.kill(record.pid, 'SIGKILL');
|
||||
} catch (error: unknown) {
|
||||
const code = (error as NodeJS.ErrnoException).code;
|
||||
if (code !== 'ESRCH') {
|
||||
logger.debug('SYSTEM', `Failed to SIGKILL session process PID ${record.pid}`, {
|
||||
pid: record.pid
|
||||
}, error as Error);
|
||||
if (error instanceof Error) {
|
||||
const code = (error as NodeJS.ErrnoException).code;
|
||||
if (code !== 'ESRCH') {
|
||||
logger.debug('SYSTEM', `Failed to SIGKILL session process PID ${record.pid}`, {
|
||||
pid: record.pid
|
||||
}, error);
|
||||
}
|
||||
} else {
|
||||
logger.warn('SYSTEM', `Failed to SIGKILL session process PID ${record.pid} (non-Error)`, {
|
||||
pid: record.pid,
|
||||
error: String(error)
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+49
-21
@@ -35,11 +35,19 @@ export async function runShutdownCascade(options: ShutdownCascadeOptions): Promi
|
||||
|
||||
try {
|
||||
await signalProcess(record.pid, 'SIGTERM');
|
||||
} catch (error) {
|
||||
logger.debug('SYSTEM', 'Failed to send SIGTERM to child process', {
|
||||
pid: record.pid,
|
||||
type: record.type
|
||||
}, error as Error);
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof Error) {
|
||||
logger.debug('SYSTEM', 'Failed to send SIGTERM to child process', {
|
||||
pid: record.pid,
|
||||
type: record.type
|
||||
}, error);
|
||||
} else {
|
||||
logger.warn('SYSTEM', 'Failed to send SIGTERM to child process (non-Error)', {
|
||||
pid: record.pid,
|
||||
type: record.type,
|
||||
error: String(error)
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,11 +57,19 @@ export async function runShutdownCascade(options: ShutdownCascadeOptions): Promi
|
||||
for (const record of survivors) {
|
||||
try {
|
||||
await signalProcess(record.pid, 'SIGKILL');
|
||||
} catch (error) {
|
||||
logger.debug('SYSTEM', 'Failed to force kill child process', {
|
||||
pid: record.pid,
|
||||
type: record.type
|
||||
}, error as Error);
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof Error) {
|
||||
logger.debug('SYSTEM', 'Failed to force kill child process', {
|
||||
pid: record.pid,
|
||||
type: record.type
|
||||
}, error);
|
||||
} else {
|
||||
logger.warn('SYSTEM', 'Failed to force kill child process (non-Error)', {
|
||||
pid: record.pid,
|
||||
type: record.type,
|
||||
error: String(error)
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,8 +84,15 @@ export async function runShutdownCascade(options: ShutdownCascadeOptions): Promi
|
||||
|
||||
try {
|
||||
rmSync(pidFilePath, { force: true });
|
||||
} catch (error) {
|
||||
logger.debug('SYSTEM', 'Failed to remove PID file during shutdown', { pidFilePath }, error as Error);
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof Error) {
|
||||
logger.debug('SYSTEM', 'Failed to remove PID file during shutdown', { pidFilePath }, error);
|
||||
} else {
|
||||
logger.warn('SYSTEM', 'Failed to remove PID file during shutdown (non-Error)', {
|
||||
pidFilePath,
|
||||
error: String(error)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
options.registry.pruneDeadEntries();
|
||||
@@ -91,10 +114,12 @@ async function signalProcess(pid: number, signal: 'SIGTERM' | 'SIGKILL'): Promis
|
||||
if (signal === 'SIGTERM') {
|
||||
try {
|
||||
process.kill(pid, signal);
|
||||
} catch (error) {
|
||||
const errno = (error as NodeJS.ErrnoException).code;
|
||||
if (errno === 'ESRCH') {
|
||||
return;
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof Error) {
|
||||
const errno = (error as NodeJS.ErrnoException).code;
|
||||
if (errno === 'ESRCH') {
|
||||
return;
|
||||
}
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
@@ -136,10 +161,12 @@ async function signalProcess(pid: number, signal: 'SIGTERM' | 'SIGKILL'): Promis
|
||||
|
||||
try {
|
||||
process.kill(pid, signal);
|
||||
} catch (error) {
|
||||
const errno = (error as NodeJS.ErrnoException).code;
|
||||
if (errno === 'ESRCH') {
|
||||
return;
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof Error) {
|
||||
const errno = (error as NodeJS.ErrnoException).code;
|
||||
if (errno === 'ESRCH') {
|
||||
return;
|
||||
}
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
@@ -151,7 +178,8 @@ async function loadTreeKill(): Promise<TreeKillFn | null> {
|
||||
try {
|
||||
const treeKillModule = await import(moduleName);
|
||||
return (treeKillModule.default ?? treeKillModule) as TreeKillFn;
|
||||
} catch {
|
||||
} catch (error: unknown) {
|
||||
logger.debug('SYSTEM', 'tree-kill module not available, using fallback', {}, error instanceof Error ? error : undefined);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user