Bug Fix Helper
Diagnoses coding bugs by analyzing error messages, code context, and expected behavior to provide targeted fixes and prevention strategies.
Coding & Development · intermediate · ChatGPT
Prompt template
I'm encountering a bug in my {{language}} application. Help me identify and fix the issue.
Error message: {{error_message}}
Relevant code:
```{{language}}
{{code_snippet}}
```
Expected behavior: {{expected_behavior}}
Actual behavior: {{actual_behavior}}
Environment: {{os_runtime_version}}
Steps to reproduce:
{{reproduction_steps}}
Please:
1. Identify the root cause of the bug
2. Explain why it's happening
3. Provide the corrected code
4. Suggest how to prevent similar bugs in the futureExample output
Root Cause: The error occurs because `Array.find()` returns `undefined` when no match is found, and you're immediately accessing `.name` on line 12 without a null check.
Fix:
```javascript
const user = users.find(u => u.id === targetId);
if (!user) {
throw new Error(`User with id ${targetId} not found`);
}
return user.name;
```
Prevention: Use TypeScript strict mode to catch potential null/undefined access at compile time.Use cases
- Developers stuck on hard-to-find bugs
- Junior developers learning debugging techniques
- Teams reducing time spent on debugging sessions
Tags
debugging, bug-fix, error-handling, troubleshooting