What it does
Flags catch blocks that contain only a `console.error()` / `console.log()` call followed immediately by rethrowing the same error — with no recovery, transformation, or additional context added.
Why AI tools generate this pattern
A catch block that only logs and rethrows adds noise but no value. The caller still receives the original error, and you've added an extra log entry that tells you nothing the stack trace doesn't already tell you. In high-volume production environments, this pattern floods logs with duplicate errors. More subtly, it makes engineers feel safe — there's error handling! — while providing no actual recovery. It's the error handling equivalent of a TODO comment.
Code Examples
try {
await paymentService.charge(amount);
} catch (err) {
console.error(err); // ← logs the error
throw err; // ← rethrows unchanged — caller sees the same error
}// Add context before rethrowing
try {
await paymentService.charge(amount);
} catch (err) {
// Wrap with additional context
throw new PaymentError(`Failed to charge ${amount}: ${err instanceof Error ? err.message : err}`, { cause: err });
}
// Or handle it properly
try {
await paymentService.charge(amount);
} catch (err) {
if (err instanceof InsufficientFundsError) {
return { success: false, reason: 'insufficient_funds' };
}
throw err; // unexpected — let it propagate
}How to Fix
- Each catch block should do one of:
- **Handle** the error and return a value
- **Transform** the error into a typed error with context (`new AppError(msg, { cause: err })`)
- **Log with context** that isn't already in the stack trace (user ID, request ID, operation name)
- Just **re-throw** without logging (the outermost handler will log it once)
Nuances & False Positive Prevention
Legitimate usage can be exempted with inline disable comments (// ai-guard-disable-next-line no-catch-log-rethrow) when appropriate.
Configuration
Enable or override this rule in your ESLint configuration:
// eslint.config.mjs
export default [
{
plugins: { 'ai-guard': aiGuard },
rules: {
'ai-guard/no-catch-log-rethrow': 'warn',
},
},
];