typescript.bare_catch
Correctness
Medium
Detects catch blocks that blindly catch all errors without proper type checking.
Why It Matters
Section titled “Why It Matters”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
Example
Section titled “Example”// ❌ 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;}What Unfault Detects
Section titled “What Unfault Detects”- catch(e) without type narrowing
- Missing instanceof checks
- No error classification logic
Auto-Fix
Section titled “Auto-Fix”Unfault can add error type guards and proper handling structure.