diff options
author | Dylan Baker <dylan@pnwbakers.com> | 2017-01-12 14:10:30 -0800 |
---|---|---|
committer | Dylan Baker <dylan@pnwbakers.com> | 2017-01-13 15:42:38 -0800 |
commit | 62674daafc5685597460d454129f207bd693a8d5 (patch) | |
tree | 491b5cc36f2970bfa078ca3a23f0739b3c9920fa | |
parent | 02fe2ad8694da6f6b3124fd78727fc86d68ba186 (diff) |
alot/ui: Handle Exceptions in UI.apply_command callbacks
This ports the same handling used in UI.apply_commandline to
UI.apply_command. This is necessary since though the first implies the
other, they are actually separate Deferred instances, and don't share a
callback/errback chain; but they can both receive a CommandCanceled
exception.
Fixes #965
-rw-r--r-- | alot/ui.py | 30 |
1 files changed, 16 insertions, 14 deletions
@@ -112,6 +112,19 @@ class UI(object): # start urwids mainloop self.mainloop.run() + def _error_handler(self, failure): + """Default handler for exceptions in callbacks.""" + if failure.check(CommandParseError): + self.notify(failure.getErrorMessage(), priority='error') + elif failure.check(CommandCanceled): + self.notify("operation cancelled", priority='error') + else: + logging.error(failure.getTraceback()) + errmsg = failure.getErrorMessage() + if errmsg: + msg = "{}\n(check the log for details)".format(errmsg) + self.notify(msg, priority='error') + def _input_filter(self, keys, raw): """ handles keypresses. @@ -229,19 +242,8 @@ class UI(object): for cmdstring in split_commandline(cmdline): d.addCallback(apply_this_command, cmdstring) - # add sequence-wide error handler - def errorHandler(failure): - if failure.check(CommandParseError): - self.notify(failure.getErrorMessage(), priority='error') - elif failure.check(CommandCanceled): - self.notify("operation cancelled", priority='error') - else: - logging.error(failure.getTraceback()) - errmsg = failure.getErrorMessage() - if errmsg: - msg = "%s\n(check the log for details)" - self.notify(msg % errmsg, priority='error') - d.addErrback(errorHandler) + d.addErrback(self._error_handler) + return d @staticmethod @@ -666,7 +668,7 @@ class UI(object): prehook = cmd.prehook or (lambda **kwargs: None) d = defer.maybeDeferred(prehook, ui=self, dbm=self.dbman, cmd=cmd) d.addCallback(call_apply) - d.addCallback(call_posthook) + d.addCallbacks(call_posthook, self._error_handler) return d def handle_signal(self, signum, frame): |