Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show error dialog when .vorta-init.json is malformed (fixes #1053) #1054

Merged
merged 2 commits into from
Aug 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/vorta/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -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!'),
Expand Down
11 changes: 9 additions & 2 deletions src/vorta/profile_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
11 changes: 6 additions & 5 deletions src/vorta/views/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/test_import_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down