Commit 75ee98ac authored by Bram Schoenmakers's avatar Bram Schoenmakers

Use raise ... from syntax to link exceptions

Python handles one exception at a time (per thread), raising a new
exception while still handling another one will result in run-time
errors. By linking the second exception to the original one, Python will
handle it correctly.

This was encountered while developing the alternative column UI, which
bailed out on completing a todo item with (invalid) child todo items.
parent 13b748ac
...@@ -71,8 +71,8 @@ class Command(object): ...@@ -71,8 +71,8 @@ class Command(object):
""" Retrieves a value from the argument list at the given position. """ """ Retrieves a value from the argument list at the given position. """
try: try:
return self.args[p_number] return self.args[p_number]
except IndexError: except IndexError as ie:
raise InvalidCommandArgument raise InvalidCommandArgument from ie
def getopt(self, p_flags, p_long=None): def getopt(self, p_flags, p_long=None):
p_long = p_long or [] p_long = p_long or []
......
...@@ -327,7 +327,7 @@ def config(p_path=None, p_overrides=None): ...@@ -327,7 +327,7 @@ def config(p_path=None, p_overrides=None):
try: try:
config.instance = _Config(p_path, p_overrides) config.instance = _Config(p_path, p_overrides)
except configparser.ParsingError as perr: except configparser.ParsingError as perr:
raise ConfigError(str(perr)) raise ConfigError(str(perr)) from perr
return config.instance return config.instance
......
...@@ -95,11 +95,11 @@ class TodoListBase(object): ...@@ -95,11 +95,11 @@ class TodoListBase(object):
# the expression is a string and no leading zeroes, # the expression is a string and no leading zeroes,
# treat it as an integer # treat it as an integer
raise TypeError raise TypeError
except TypeError: except TypeError as te:
try: try:
result = self._todos[int(p_identifier) - 1] result = self._todos[int(p_identifier) - 1]
except (ValueError, IndexError): except (ValueError, IndexError):
raise InvalidTodoException raise InvalidTodoException from te
return result return result
...@@ -249,8 +249,8 @@ class TodoListBase(object): ...@@ -249,8 +249,8 @@ class TodoListBase(object):
return self._todo_id_map[p_todo] return self._todo_id_map[p_todo]
else: else:
return self._todos.index(p_todo) + 1 return self._todos.index(p_todo) + 1
except (ValueError, KeyError): except (ValueError, KeyError) as ex:
raise InvalidTodoException raise InvalidTodoException from ex
def _update_todo_ids(self): def _update_todo_ids(self):
# the idea is to have a hash that is independent of the position of the # the idea is to have a hash that is independent of the position of the
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment