🔴 Reading: Error Messages & Logs
3 exercises — read real Python stack traces, application logs, and error messages. Identify what went wrong, on which line, and why.
Reading stack traces: top-down or bottom-up?
- Read bottom to top: the error type and message are at the bottom
- The line just above is where the error actually occurred
- Read upward to trace the call chain
- In logs: read chronologically — events tell a story
0 / 3 completed
1 / 3
Python Stack Trace
Traceback (most recent call last):
File "app/services/order_service.py", line 87, in process_order
total = calculate_total(items)
File "app/utils/pricing.py", line 34, in calculate_total
return sum(item['price'] * item['quantity'] for item in items)
File "app/utils/pricing.py", line 34, in <listcomp>
return sum(item['price'] * item['quantity'] for item in items)
KeyError: 'price' Read the stack trace above. Which statement accurately describes what happened?
KeyError: 'price' — a missing dictionary key:
Reading a Python stack trace from bottom to top gives you the error and its location:
① Error type:
② Error location: Line 34 in
③ Call chain (top to bottom):
1.
2.
Why was the list empty not the issue? If items were empty,
Stack trace reading strategy:
Reading a Python stack trace from bottom to top gives you the error and its location:
① Error type:
KeyError: 'price' — Python raises a KeyError when you try to access a dictionary key that doesn't exist. The key 'price' is not present in at least one item.② Error location: Line 34 in
pricing.py — inside the calculate_total() function, in the list comprehension: item['price'] * item['quantity']③ Call chain (top to bottom):
1.
order_service.py:87 → called calculate_total(items)2.
pricing.py:34 → tried to access item['price'] → KeyErrorWhy was the list empty not the issue? If items were empty,
sum() would return 0, not raise an error. The issue is a specific item missing the key.Stack trace reading strategy:
- Start at the bottom — the actual error type and message
- The line just above is where it failed
- Read upward to find the call chain — what triggered what
- The top is the outermost entry point