From de28422dbfcec32ae83a25e37c93309921c7d607 Mon Sep 17 00:00:00 2001 From: Philipp Hossner Date: Wed, 11 Aug 2021 22:10:15 +0200 Subject: [PATCH 1/2] Show error dialog when .vorta-init.json is malformed (fixes #1053) --- src/vorta/application.py | 18 ++++++++++++++++-- src/vorta/profile_export.py | 11 +++++++++-- src/vorta/views/main_window.py | 11 ++++++----- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src/vorta/application.py b/src/vorta/application.py index bb8ae0ccc..12b1cd087 100644 --- a/src/vorta/application.py +++ b/src/vorta/application.py @@ -257,8 +257,22 @@ def bootstrap_profile(self, bootstrap_file=PROFILE_BOOTSTRAP_FILE): or add an empty "Default" profile. """ if bootstrap_file.is_file(): - profile_export = ProfileExport.from_json(bootstrap_file) - profile = profile_export.to_db(overwrite_profile=True, overwrite_settings=True) + try: + profile_export = ProfileExport.from_json(bootstrap_file) + profile = profile_export.to_db(overwrite_profile=True, overwrite_settings=True) + except Exception as exception: + double_newline = os.linesep + os.linesep + QMessageBox.critical(None, + self.tr('Failed to import profile'), + "{}{}\"{}\"{}{}".format( + self.tr('Failed to import a profile from {}:').format(bootstrap_file), + double_newline, + str(exception), + double_newline, + self.tr('Consider removing or repairing this file to ' + 'get rid of this message.'), + )) + return bootstrap_file.unlink() notifier = VortaNotifications.pick() notifier.deliver(self.tr('Profile import successful!'), diff --git a/src/vorta/profile_export.py b/src/vorta/profile_export.py index a138a5a5e..421936e36 100644 --- a/src/vorta/profile_export.py +++ b/src/vorta/profile_export.py @@ -145,8 +145,10 @@ def from_json(cls, filename): with open(filename, 'r') as file: try: profile_export = ProfileExport(json.loads(file.read())) - except JSONDecodeError: - return None + except JSONDecodeError as exception: + raise ImportFailedException( + 'This file does not contain valid JSON: {}'.format(str(exception)) + ) from exception return profile_export def to_json(self): @@ -161,3 +163,8 @@ def _converter(obj): class VersionException(Exception): """ For when current_version < export_version. Should only occur if downgrading """ pass + + +class ImportFailedException(Exception): + """Raised when a profile could not be imported.""" + pass diff --git a/src/vorta/views/main_window.py b/src/vorta/views/main_window.py index aecaca59a..1e62373fd 100644 --- a/src/vorta/views/main_window.py +++ b/src/vorta/views/main_window.py @@ -10,7 +10,7 @@ from vorta.utils import borg_compat, get_asset, is_system_tray_available, get_network_status_monitor from vorta.views.partials.loading_button import LoadingButton from vorta.views.utils import get_colored_icon -from vorta.profile_export import ProfileExport +from vorta.profile_export import ProfileExport, ImportFailedException from .archive_tab import ArchiveTab from .export_window import ExportWindow from .import_window import ImportWindow @@ -217,11 +217,12 @@ def profile_imported_event(profile): str(Path.home()), self.tr("JSON (*.json);;All files (*)"))[0] if filename: - profile_export = ProfileExport.from_json(filename) - if profile_export is None: + try: + profile_export = ProfileExport.from_json(filename) + except ImportFailedException as exception: QMessageBox.critical(None, - self.tr('Error'), - self.tr('This file does not contain valid JSON.')) + self.tr('Failed to import profile'), + self.tr(str(exception))) return window = ImportWindow(profile_export=profile_export) self.window = window From 4ccb5a2efc288cb77bc3d44622517106022caecd Mon Sep 17 00:00:00 2001 From: Philipp Hossner Date: Wed, 11 Aug 2021 22:21:01 +0200 Subject: [PATCH 2/2] Fix test --- tests/test_import_export.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_import_export.py b/tests/test_import_export.py index ecc515da0..9ae3b75a5 100644 --- a/tests/test_import_export.py +++ b/tests/test_import_export.py @@ -72,7 +72,7 @@ def critical(widget, title, message): main.profile_import_action() # assert somehow that an alert is shown - assert alert_message == 'This file does not contain valid JSON.' + assert alert_message == 'This file does not contain valid JSON: Expecting value: line 1 column 1 (char 0)' def test_export_success(qapp, qtbot, tmpdir, monkeypatch):