diff --git a/telegram/ext/conversationhandler.py b/telegram/ext/conversationhandler.py index 19bc99e78..73e0a2857 100644 --- a/telegram/ext/conversationhandler.py +++ b/telegram/ext/conversationhandler.py @@ -128,10 +128,16 @@ class ConversationHandler(Handler): self.logger.debug('waiting for promise...') old_state, new_state = state - new_state.result(timeout=self.run_async_timeout) + error = False + try: + res = new_state.result(timeout=self.run_async_timeout) + except Exception as exc: + self.logger.exception("promise function raised exception: %s" % + exc) + error = True - if new_state.done.is_set(): - self.update_state(new_state.result(), key) + if not error and new_state.done.is_set(): + self.update_state(res, key) state = self.conversations.get(key) else: diff --git a/telegram/ext/dispatcher.py b/telegram/ext/dispatcher.py index 37e6c8a5c..c551a33d4 100644 --- a/telegram/ext/dispatcher.py +++ b/telegram/ext/dispatcher.py @@ -159,11 +159,7 @@ class Dispatcher(object): len(self.__async_threads)) break - try: - promise.run() - - except: - self.logger.exception("run_async function raised exception") + promise.run() def run_async(self, func, *args, **kwargs): """Queue a function (with given args/kwargs) to be run asynchronously. diff --git a/telegram/utils/promise.py b/telegram/utils/promise.py index e2a8ff9fd..3c3207fc4 100644 --- a/telegram/utils/promise.py +++ b/telegram/utils/promise.py @@ -30,17 +30,23 @@ class Promise(object): self.kwargs = kwargs self.done = Event() self._result = None + self._exception = None def run(self): try: self._result = self.pooled_function(*self.args, **self.kwargs) - except: - raise + except Exception as exc: + self._exception = exc finally: self.done.set() + def __call__(self): + self.run() + def result(self, timeout=None): self.done.wait(timeout=timeout) + if self._exception is not None: + raise self._exception return self._result