Checkpoint: A Parser That Explains Itself
intermediate35 min readLesson 121 of 204
A config parser with a custom exception carrying the real line number — structured errors end to end.
parse_config_value is the module's exam: it combines custom exception
design (Module lesson 2), line-based parsing (lesson 4), and accurate
diagnostics.
The three failure modes your parser must distinguish:
- a
value=line whose payload is empty or non-numeric →ConfigErrorwith the real line number - no
value=line at all →ConfigErrorwith the missing-value message - a valid line → return the number
The line counter is the part that fails casually-written solutions:
increment per line before inspecting it, and count the final line even
when the content does not end with \n.
Tests assert on e.line_no() as a string field — proof that structured
exceptions carry more than a message can.