diff --git a/nlightreader/utils/threads.py b/nlightreader/utils/threads.py index 9d5cae8..ff76b6b 100644 --- a/nlightreader/utils/threads.py +++ b/nlightreader/utils/threads.py @@ -1,3 +1,4 @@ +import traceback from typing import Callable from PySide6.QtCore import ( @@ -11,6 +12,7 @@ class Signals(QObject): + error = Signal(Exception) finished = Signal(object) @@ -22,6 +24,7 @@ def __init__( kwargs=None, *, callback=None, + error_callback=None, ): super().__init__() if kwargs is None: @@ -32,11 +35,18 @@ def __init__( self.signals = Signals() if callback: self.signals.finished.connect(callback) + if error_callback: + self.signals.error.connect(error_callback) @Slot() def run(self): - result = self._target(*self._args, **self._kwargs) - self.signals.finished.emit(result) + try: + result = self._target(*self._args, **self._kwargs) + except Exception as e: + traceback.print_exc() + self.signals.error.emit(e) + else: + self.signals.finished.emit(result) class Worker(NlThread, QRunnable): @@ -66,8 +76,15 @@ def __init__( kwargs=None, *, callback=None, + error_callback=None, ): - super().__init__(target, args, kwargs, callback=callback) + super().__init__( + target, + args, + kwargs, + callback=callback, + error_callback=error_callback, + ) def start(self, pool=None): if pool is None: @@ -104,5 +121,12 @@ def __init__( kwargs=None, *, callback=None, + error_callback=None, ): - super().__init__(target, args, kwargs, callback=callback) + super().__init__( + target, + args, + kwargs, + callback=callback, + error_callback=error_callback, + )