Handle malformed .vorta-init.json on import. Fixes #1053. By @phihos (#1054)

Show error dialog when .vorta-init.json is malformed.
This commit is contained in:
Philipp Hossner 2021-08-12 06:32:08 +02:00 committed by GitHub
parent 7d2dbe001d
commit ff010b3bc4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 10 deletions

View File

@ -257,8 +257,22 @@ class VortaApp(QtSingleApplication):
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!'),

View File

@ -145,8 +145,10 @@ class ProfileExport:
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 @@ class ProfileExport:
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

View File

@ -10,7 +10,7 @@ from vorta.models import BackupProfileModel, SettingsModel
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 @@ class MainWindow(MainWindowBase, MainWindowUI):
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

View File

@ -72,7 +72,7 @@ def test_import_fail_not_json(qapp, rootdir, monkeypatch):
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):