Exercise: Errors

Good errors say where and what. "line 3: 'port' must be a whole number, got 'eighty'" beats ValueError: invalid literal for int().

Exercise: finish parse_config. It reads lines of key = value and returns a dict of str to int.

  • Skip blank lines and lines starting with #. Strip spaces around keys and values. workers=4 is fine too.
  • A line with no =, or an empty key, raises ConfigError("line 2: expected 'key = value', got 'oops'").
  • A value that isn't an int raises ConfigError("line 1: 'port' must be a whole number, got 'eighty'"), chained with from to the original ValueError.
  • A repeated key raises ConfigError("line 3: duplicate key 'a'").

Line numbers start at 1. Show the stripped line, key and value with !r.

Also make ConfigError a subclass of ValueError, so callers that already catch ValueError still work.

Press Check to run the tests.