capture errors and add basic handle of the crash

This commit is contained in:
Jakub Trllo 2024-06-18 12:02:22 +02:00
parent 2322d0f2ad
commit 59e49182be
2 changed files with 24 additions and 4 deletions

View file

@ -192,7 +192,11 @@ class FoldersQtModel(QtGui.QStandardItemModel):
or thread_id != self._current_refresh_thread.id or thread_id != self._current_refresh_thread.id
): ):
return return
folder_items, folder_type_items = thread.get_result() if thread.failed:
# TODO visualize that refresh failed
folder_items, folder_type_items = {}, {}
else:
folder_items, folder_type_items = thread.get_result()
self._fill_items(folder_items, folder_type_items) self._fill_items(folder_items, folder_type_items)
self._current_refresh_thread = None self._current_refresh_thread = None

View file

@ -2,7 +2,10 @@ import os
import sys import sys
import contextlib import contextlib
import collections import collections
import traceback
from functools import partial from functools import partial
from typing import Union, Any
from qtpy import QtWidgets, QtCore, QtGui from qtpy import QtWidgets, QtCore, QtGui
import qtawesome import qtawesome
@ -425,26 +428,39 @@ class RefreshThread(QtCore.QThread):
self._id = thread_id self._id = thread_id
self._callback = partial(func, *args, **kwargs) self._callback = partial(func, *args, **kwargs)
self._exception = None self._exception = None
self._traceback = None
self._result = None self._result = None
self.finished.connect(self._on_finish_callback) self.finished.connect(self._on_finish_callback)
@property @property
def id(self): def id(self) -> str:
return self._id return self._id
@property @property
def failed(self): def failed(self) -> bool:
return self._exception is not None return self._exception is not None
def run(self): def run(self):
try: try:
self._result = self._callback() self._result = self._callback()
except Exception as exc: except Exception as exc:
exc_type, exc_value, exc_traceback = sys.exc_info()
err_traceback = "".join(traceback.format_exception(
exc_type, exc_value, exc_traceback
))
print(err_traceback)
self._traceback = err_traceback
self._exception = exc self._exception = exc
def get_result(self): def get_result(self) -> Any:
return self._result return self._result
def get_exception(self) -> Union[BaseException, None]:
return self._exception
def get_traceback(self) -> Union[str, None]:
return self._traceback
def _on_finish_callback(self): def _on_finish_callback(self):
"""Trigger custom signal with thread id. """Trigger custom signal with thread id.