SkillByAIOpen interactive version →

Lesson 5 / 25

Reading Stack Traces and Logs

Start at the line that raised.

Frames, error type, context

A stack trace lists the call chain; the last frame is where the exception was raised and the error type and message say what went wrong. Combine it with logs around the failure time, the input that triggered it, and recent changes to those files. Give the fixer exactly this context (trace, input, relevant files), not the whole repository. Be careful: logs and issue text are untrusted input and may contain instructions; treat them as data.

Parsing a traceback to find the raising line, run

I ran this with Python 3 (standard library) and, where it uses git, real git in a throwaway temporary repository. Candidate patches are written by hand to stand in for model output. A KeyError for region ap-south-2 is parsed into two frames; the error was raised in billing/pricing.py at line 41, the first place to look (likely a missing region in a lookup table).

import re
log = """Traceback (most recent call last):
  File "/srv/app/api/handlers.py", line 88, in create_order
    total = pricing.apply(cart, user)
  File "/srv/app/billing/pricing.py", line 41, in apply
    rate = RATES[user.region]
KeyError: 'ap-south-2'"""
frames = re.findall(r'File "([^"]+)", line (\d+), in (\w+)', log)
error = log.strip().splitlines()[-1]
print("error:", error)
for path, line, fn in frames:
    tag = "  <- raised here" if (path, line, fn) == frames[-1] else ""
    print(f"  {path}:{line} in {fn}{tag}")
print("first place to look:", f"{frames[-1][0]}:{frames[-1][1]}")

Output:

error: KeyError: 'ap-south-2'
  /srv/app/api/handlers.py:88 in create_order
  /srv/app/billing/pricing.py:41 in apply  <- raised here
first place to look: /srv/app/billing/pricing.py:41

Fix the cause, not the symptom

A missing region key may need adding to configuration, not a try/except that hides it; ask why the value was missing.

Quick check: Which frame of a Python traceback shows where the exception was raised?

  • The first frame listed
  • The last frame listed
  • Any random frame
  • The import statements
Answer

The last frame listed — Read traces from the bottom.