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:
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
}
Enter the URL of a public GitHub repository