Skip to main content
Reading progress: 0%

Lesson 7.1: Errors, Logs, Reproduction, and Root-Cause Debugging

Lesson Quick-Jump (10 Sections)

1. What Are We Learning?​

In this lesson, you will learn how to:

  • Read error messages and stack traces without panicking.
  • Inspect server logs and browser console/network logs.
  • Reproduce bugs reliably before trying to fix them.
  • Ask AI agents to investigate the root cause rather than slapping on random band-aids.
  • Verify proposed fixes properly.

2. Why Does It Matter?​

info

When something breaks, beginners often paste "It doesn't work, fix it" into an AI agent. Without knowing how to reproduce the bug or read logs, the AI starts guessing wildly—adding try/catch blocks that hide errors, changing random files, or breaking working features while trying to fix the symptom.

Systematic debugging saves hours and turns scary red error messages into helpful clues.


3. Simple Explanation for a Complete Beginner​

Think of a car mechanic diagnosing an engine rattle:

  • Reproducing the bug means figuring out exactly how to trigger it every time ("It happens only when clicking Save with a blank date"). If you can't trigger it consistently, you won't know when it's truly fixed.
  • Logs are your app's flight recorder. Two places always show clues:
    1. Browser Developer Tools (DevTools) (F12 or Right-Click -> Inspect -> Console and Network tabs) show frontend errors and failed API requests (like 400 Bad Request or 500 Internal Server Error).
    2. Terminal / Server Logs show backend crashes and database errors.
  • A Stack Trace looks scary because it prints 15–30 lines of file paths, but it's just a breadcrumb trail showing where the crash happened. Always read the top error message and look for your own file names (ignoring internal library folders like node_modules/).
  • Root Cause vs. Symptom:
    • Symptom: "The page stays blank after clicking Save."
    • Root Cause: "The backend returned 500 because created_at was missing in the database insert." Fixing the symptom (e.g., hiding the error) leaves the data broken; fixing the root cause solves the real bug.

4. Practical Example​

Suppose clicking Add Habit shows nothing on screen.

What a beginner might prompt (Bad):

"Adding a habit doesn't work. Fix it!"

Systematic Investigation:

  1. Reproduce: Type "Stretch", click Add, refresh -> habit is not there. Happens every time.
  2. Check Browser DevTools -> Network tab: Click the red POST /api/habits request -> Status is 500 Internal Server Error.
  3. Check Terminal (Backend Logs):
    TypeError: Cannot read properties of undefined (reading 'title')
    at createHabit (src/routes/habits.js:18:29)
  4. Aha! Look at src/routes/habits.js line 18: req.body is undefined because JSON body parsing middleware wasn't enabled in the server setup!
  5. Now we have the exact evidence to give the AI agent (or write a clear bug report using templates/bug-report-template.md).

5. Step-by-Step Instructions​

  1. Reproduce the bug and write down the exact steps: What did you click? What did you type? What did you expect? What actually happened?
  2. Collect evidence from BOTH logs:
    • Open Browser DevTools -> Console tab (look for red errors) and Network tab (look for red failed requests and check their Status code & Response).
    • Check your Terminal running the backend server and copy the exact error message + stack trace.
  3. Ask the AI to investigate root cause FIRST—without editing files yet.
  4. Read the AI's root-cause explanation. Does it match the exact line number and error you saw?
  5. Approve the minimal fix, then test BOTH:
    • Your original reproduction steps (is the bug gone?)
    • Nearby features (did the fix break anything else?)

6. Ready-to-Use AI Prompt​

Context:
I found a bug in the app. I reproduced it and collected the error logs below.
- Expected behavior: [what should happen]
- Actual behavior: [what happened instead]
- Steps to reproduce:
1. [step 1]
2. [step 2]
- Browser Console / Network error: [paste exact message & HTTP status, or "none"]
- Server Terminal stack trace: [paste exact error lines]

Objective:
Inspect the relevant files and identify the exact ROOT CAUSE of this bug before making any edits.

Boundaries:
- Do NOT edit any files yet.
- Do NOT swallow errors (no empty try/catch blocks) or disable validation to make the error disappear.
- Stick strictly to diagnosing why this happens.

Expected Output:
1. Plain-English explanation of the root cause, citing exact file paths and line numbers.
2. Why the previous code behaved this way.
3. A minimal, targeted fix plan and how we will verify both the fix and that nothing else broke.

7. What to Expect from the AI Agent​

Good ResponseWarning Sign
The AI traces the data flow from your click -> API call -> backend line number, explains the exact mismatch (e.g., wrong field name habitTitle vs. title), and proposes a 2-line fix plus a test check.The AI guesses blindly without reading the file ("Try reinstalling node_modules" or "Let's rewrite the whole component"), OR hides the error with try { ... } catch (e) {}. Always reject band-aids that hide real failures.

8. What You Must Personally Verify​

  • Re-run the exact failing steps yourself after the fix—do not just take the AI's word that it's fixed.
  • Check the logs again: Confirm the red error in Browser DevTools and Terminal is truly gone (not just hidden).
  • Check for regressions: Test 2–3 related actions (e.g., editing and deleting, not just adding) to make sure the fix didn't break neighbors.

9. Common Mistakes​

  1. Describing bugs vaguely without logs: "It's broken" forces the AI to guess. Always include steps to reproduce + exact error text.
  2. Pasting only the last line of an error: Copy the full relevant stack trace (at least the error line and the first 10–15 lines showing your project's files).
  3. Stacking 3 fixes when the 1st didn't work: If a proposed fix fails, revert it with Git before trying a different idea—otherwise leftover failed edits pollute your code.

10. Small Practical Exercise​

Pick a small test bug (or intentionally break one label/endpoint locally on a test branch, such as sending an empty request or misspelling an API path like /api/habts):

  1. Trigger the failure and inspect Browser DevTools (Console + Network) and your Server Terminal.
  2. Fill out templates/bug-report-template.md with what you observed.
  3. Run the Root-Cause Investigation prompt above and verify the AI points to the exact file and line you broke.

Completed the exercise? Track your progress

Check off this lesson in PROGRESS-CHECKLIST.md once you have verified every step in Section 8.