From 5380066cdfdc9d6890e8cd99dfec33093b597371 Mon Sep 17 00:00:00 2001 From: drew2a Date: Thu, 8 Sep 2022 16:59:32 +0200 Subject: [PATCH] Protect `show_add_torrent_to_channel_dialog_from_uri` from `UnicodeDecodeError` --- src/tribler/gui/tribler_request_manager.py | 1 + src/tribler/gui/tribler_window.py | 13 +++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/tribler/gui/tribler_request_manager.py b/src/tribler/gui/tribler_request_manager.py index b877e547262..452b6299097 100644 --- a/src/tribler/gui/tribler_request_manager.py +++ b/src/tribler/gui/tribler_request_manager.py @@ -222,6 +222,7 @@ def on_finished(self, request): and not TriblerRequestManager.window.core_manager.shutting_down ): # TODO: Report REST API errors to Sentry + logging.error(f'REST API error: {json_result}') request_manager.show_error(TriblerRequestManager.get_message_from_error(json_result)) else: self.received_json.emit(json_result) diff --git a/src/tribler/gui/tribler_window.py b/src/tribler/gui/tribler_window.py index ff414aa0fc5..ab24cc5ebe7 100644 --- a/src/tribler/gui/tribler_window.py +++ b/src/tribler/gui/tribler_window.py @@ -42,7 +42,6 @@ QSystemTrayIcon, QTreeWidget, ) - from psutil import LINUX from tribler.core.upgrade.version_manager import VersionHistory @@ -84,8 +83,8 @@ from tribler.gui.dialogs.new_channel_dialog import NewChannelDialog from tribler.gui.dialogs.startdownloaddialog import StartDownloadDialog from tribler.gui.error_handler import ErrorHandler -from tribler.gui.exceptions import TriblerGuiTestException from tribler.gui.event_request_manager import EventRequestManager +from tribler.gui.exceptions import TriblerGuiTestException from tribler.gui.tribler_action_menu import TriblerActionMenu from tribler.gui.tribler_request_manager import ( TriblerNetworkRequest, @@ -681,8 +680,14 @@ def on_add_button_pressed(channel_id): scheme = scheme_from_url(uri) if scheme == FILE_SCHEME: file_path = url_to_path(uri) - with open(file_path) as torrent_file: - post_data['torrent'] = b64encode(torrent_file.read()).decode('utf8') + content = Path(file_path).read_bytes() + try: + post_data['torrent'] = b64encode(content).decode('utf8') + except UnicodeDecodeError as e: + self._logger.error(f'{e.__class__.__name__}: {e}') + error_message = f'Can not add the torrent to the personal channel: {e}' + ConfirmationDialog.show_message(self.window(), tr('Error'), tr(error_message), "OK") + elif scheme == MAGNET_SCHEME: post_data['uri'] = uri