Reviewer · Error Handling
Handle errors with specificity
Always handle errors specifically and explicitly, avoiding generic catch blocks that mask or ignore errors. Ensure proper resource cleanup and consistent error propagation. Key practices:
Always handle errors specifically and explicitly, avoiding generic catch blocks that mask or ignore errors. Ensure proper resource cleanup and consistent error propagation. Key practices:
- Match specific error conditions instead of using catch-all blocks
- Clean up resources even in error paths
- Propagate errors appropriately rather than swallowing them
- Handle errors consistently across similar scenarios
Example of proper error handling:
private async getWorktreesFS(): Promise<Worktree[]> {
const worktreesPath = path.join(this.repositoryRoot, '.git', 'worktrees');
try {
const raw = await fs.readdir(worktreesPath);
// ... processing ...
} catch (err) {
// Handle specific error condition
if (/ENOENT/.test(err.message)) {
return [];
}
// Propagate unexpected errors
throw err;
} finally {
// Clean up resources
this.disposables.dispose();
}
}
Bad practice to avoid:
try {
// ... operations ...
} catch (err) {
// DON'T: Silently swallow errors
}