Skip to content

typescript.bare_catch

Correctness Medium

Detects catch blocks that blindly catch all errors without proper type checking.

Bare catch blocks:

  • Swallow important errors — Can’t distinguish error types
  • Mask bugs — Programming errors caught as expected errors
  • Poor recovery — Wrong error handling logic applied
// ❌ Before (bare catch)
try {
await fetchData();
} catch (e) {
console.log("Failed to fetch"); // What if 'e' is a TypeError?
}
// ✅ After (typed error handling)
import { isAxiosError } from 'axios';
try {
await fetchData();
} catch (error) {
if (isAxiosError(error)) {
if (error.response?.status === 404) {
return null;
}
logger.error('API error', { status: error.response?.status });
} else if (error instanceof Error) {
logger.error('Unexpected error', { message: error.message });
}
throw error;
}
  • catch(e) without type narrowing
  • Missing instanceof checks
  • No error classification logic

Unfault can add error type guards and proper handling structure.