diff --git a/taskw/exceptions.py b/taskw/exceptions.py new file mode 100644 index 0000000..5118136 --- /dev/null +++ b/taskw/exceptions.py @@ -0,0 +1,8 @@ + + +class TaskwarriorError(Exception): + def __init__(self, stderr, stdout, code): + self.stderr = stderr.strip() + self.stdout = stdout.strip() + self.code = code + super(TaskwarriorError, self).__init__(self.stderr) diff --git a/taskw/warrior.py b/taskw/warrior.py index 341e5a8..3d8f049 100644 --- a/taskw/warrior.py +++ b/taskw/warrior.py @@ -24,6 +24,7 @@ import pprint import taskw.utils +from taskw.exceptions import TaskwarriorError import six from six import with_metaclass @@ -420,11 +421,15 @@ def _execute(self, *args): 'rc.verbose=nothing', 'rc.confirmation=no', ] + [six.text_type(arg) for arg in args] - return subprocess.Popen( + proc = subprocess.Popen( command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, - ).communicate() + ) + stdout, stderr = proc.communicate() + if proc.returncode != 0: + raise TaskwarriorError(stderr, stdout, proc.returncode) + return stdout, stderr def _get_json(self, *args): encoded = self._execute(*args)[0]