question 238 Debugging & Logging · medium
The error that was swallowed
total_weight should add up the weight_kg of a shipment's rows, skipping rows whose weight isn't a number. It returns 0 for every shipment, and nothing in the log explains why. The except Exception: pass is hiding the real error. Make the error visible (narrow the except), find the actual bug, and fix it so the function meets its contract.
Function
total_weight(rows), using the logger log = logging.getLogger("weights")Returnsthe sum of the rows'
weight_kg values as numbersBad valuea
weight_kg that isn't a number is skipped with WARNING row <n>: bad weight '<text>' (rows counted from 1)Missing columna row with no
weight_kg key means the file's format changed: the KeyError must reach the callerExamples
calltotal_weight([{'sku': '21329', 'weight_kg': '1.5'}, {'sku': '85048', 'weight_kg': 'n/a'}, {'sku': '22139', 'weight_kg': '0.25'}])returns1.75