---
name: explain-error
description: Deep dive into any error message or stack trace — find root cause and provide exact fix
user-invocable: true
allowed-tools: Read, Grep, Glob, Bash
---

Diagnose and explain an error. Paste the error message or stack trace as $ARGUMENTS, or describe what went wrong.

Error/description: $ARGUMENTS

1. **Parse the error**:
   - Extract error type, message, and stack frames
   - Identify the first non-framework frame (the user's own code) in the stack trace

2. **Read the source**: Open the file and line number from the stack trace. Read surrounding context (20 lines).

3. **Trace the cause**:
   - Follow the call chain upward through the stack frames
   - Read each relevant file to understand what state/data led to this error
   - Check for recent changes in git: `git log --oneline -10 -- <file>`

4. **Explain in plain English**:
   - What went wrong (not just the error message — the actual cause)
   - Why it happened (the conditions that triggered it)
   - Where in the code it originates

5. **Provide the fix**:
   - Show the exact code change needed
   - If multiple approaches exist, explain the tradeoffs
   - If it's a data issue (null/undefined), show both the guard AND where the data should be fixed upstream

6. **Prevent recurrence**: Suggest a test case that would catch this error in future.
