X Tutup
Skip to content

Commit fb37877

Browse files
author
thodnev
committed
Changed promises to handle exceptions
1 parent 264b9bd commit fb37877

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

telegram/ext/conversationhandler.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,16 @@ def check_update(self, update):
128128
self.logger.debug('waiting for promise...')
129129

130130
old_state, new_state = state
131-
new_state.result(timeout=self.run_async_timeout)
132-
133-
if new_state.done.is_set():
134-
self.update_state(new_state.result(), key)
131+
error = False
132+
try:
133+
res = new_state.result(timeout=self.run_async_timeout)
134+
except Exception as exc:
135+
self.logger.exception("promise function raised exception: %s" %
136+
exc)
137+
error = True
138+
139+
if not error and new_state.done.is_set():
140+
self.update_state(res, key)
135141
state = self.conversations.get(key)
136142

137143
else:

telegram/ext/dispatcher.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,7 @@ def _pooled(self):
159159
len(self.__async_threads))
160160
break
161161

162-
try:
163-
promise.run()
164-
165-
except:
166-
self.logger.exception("run_async function raised exception")
162+
promise.run()
167163

168164
def run_async(self, func, *args, **kwargs):
169165
"""Queue a function (with given args/kwargs) to be run asynchronously.

telegram/utils/promise.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,23 @@ def __init__(self, pooled_function, args, kwargs):
3030
self.kwargs = kwargs
3131
self.done = Event()
3232
self._result = None
33+
self._exception = None
3334

3435
def run(self):
3536
try:
3637
self._result = self.pooled_function(*self.args, **self.kwargs)
3738

38-
except:
39-
raise
39+
except Exception as exc:
40+
self._exception = exc
4041

4142
finally:
4243
self.done.set()
4344

45+
def __call__(self):
46+
self.run()
47+
4448
def result(self, timeout=None):
4549
self.done.wait(timeout=timeout)
50+
if self._exception is not None:
51+
raise self._exception
4652
return self._result

0 commit comments

Comments
 (0)
X Tutup