[gnome-boxes/wip/text-editor: 8/8] text-editor: Prevent crash when the underlying config file is missing




commit a657eb048545dbd4734ab8ba0333716e5ed601b8
Author: Felipe Borges <felipeborges gnome org>
Date:   Fri Aug 7 15:57:01 2020 +0200

    text-editor: Prevent crash when the underlying config file is missing
    
    We backup the original file in the respective XDG_CONFIG_DIR, but if
    for some reason the configuration file gets deleted or corrupted, we
    shouldn't crash neither allow the user to overwrite the existing
    configuration.

 src/text-editor.vala | 26 +++++++++++++++++++++-----
 1 file changed, 21 insertions(+), 5 deletions(-)
---
diff --git a/src/text-editor.vala b/src/text-editor.vala
index 6b53129b..6bbc2abb 100644
--- a/src/text-editor.vala
+++ b/src/text-editor.vala
@@ -38,7 +38,12 @@ public async void save () {
         }
 
         var saved = yield save_original_config (config);
+        if (!saved) {
+            var failed_to_save_msg = _("Unable to backup original configuration. Aborting.");
+            App.app.main_window.notificationbar.display_error (failed_to_save_msg);
 
+            return;
+        }
 
         var xml = view.buffer.text;
         if (config.to_xml () == xml) {
@@ -46,7 +51,6 @@ public async void save () {
             return;
         }
 
-
         GVirConfig.Domain? custom_config = null;
         try {
             custom_config = new GVirConfig.Domain.from_xml (xml);
@@ -87,16 +91,28 @@ private void add_metadata (GVirConfig.Domain config) {
     private async bool save_original_config (GVirConfig.Domain config) {
         var old_config_path = get_user_pkgconfig (config.get_name () + FILE_SUFFIX);
 
-        return FileUtils.set_contents (old_config_path, config.to_xml (), -1);
+        try {
+            return FileUtils.set_contents (old_config_path, config.to_xml (), -1);
+        } catch (GLib.Error error) {
+            warning ("Failed to save original configuration: %s", error.message);
+
+            return false;
+        }
     }
 
     public async void revert_to_original () {
         var original_config_path = get_user_pkgconfig (machine.domain_config.get_name () + FILE_SUFFIX);
 
-        string data;
-        FileUtils.get_contents (original_config_path, out data);
+        string? data = null;
+        try {
+            FileUtils.get_contents (original_config_path, out data);
+        } catch (GLib.Error error) {
+            warning ("Failed to load original configuration: %s", error.message);
+            return;
+        }
+
         if (data == null) {
-            warning ("Failed to load original config");
+            warning ("Failed to load original configuration");
             return;
         }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]