[ghex] Error handling improvements



commit 54daaef7bfcf2584eecdd7c75896f7edfdddad59
Author: Logan Rathbone <poprocks gmail com>
Date:   Thu Jun 16 18:02:29 2022 -0400

    Error handling improvements
    
    mmap: Clear errors when required
    appwin: Use hex-document async functions so we get access to GError.

 src/ghex-application-window.c | 109 +++++++++++++++++++++++++++++-------------
 src/hex-buffer-mmap.c         |   3 ++
 2 files changed, 79 insertions(+), 33 deletions(-)
---
diff --git a/src/ghex-application-window.c b/src/ghex-application-window.c
index 65e591a..8753a01 100644
--- a/src/ghex-application-window.c
+++ b/src/ghex-application-window.c
@@ -313,6 +313,34 @@ ghex_application_window_remove_tab (GHexApplicationWindow *self,
                show_no_file_loaded_label (self);
 }
 
+static void
+file_save_write_cb (HexDocument *doc,
+               GAsyncResult *res,
+               GHexApplicationWindow *self)
+{
+       GError *local_error = NULL;
+       gboolean write_successful;
+
+       write_successful = hex_document_write_finish (doc, res, &local_error);
+
+       if (write_successful)
+       {
+               g_debug ("%s: File saved successfully.", __func__);
+       }
+       else
+       {
+               GString *full_errmsg = g_string_new (
+                               _("There was an error saving the file."));
+
+               if (local_error)
+                       g_string_append_printf (full_errmsg, "\n\n%s", local_error->message);
+
+               display_error_dialog (GTK_WINDOW(self), full_errmsg->str);
+
+               g_string_free (full_errmsg, TRUE);
+       }
+}
+
 static void
 file_save (GHexApplicationWindow *self)
 {
@@ -321,17 +349,10 @@ file_save (GHexApplicationWindow *self)
        doc = hex_widget_get_document (ACTIVE_GH);
        g_return_if_fail (HEX_IS_DOCUMENT (doc));
 
-       if (hex_document_write (doc)) {
-               /* we're happy... */
-               g_debug ("%s: File saved successfully.", __func__);
-       }
-       else {
-               display_error_dialog (GTK_WINDOW(self),
-                               _("There was an error saving the file."
-                               "\n\n"
-                               "Your permissions of the file may have been changed "
-                               "by another program, or the file may have become corrupted."));
-       }
+       hex_document_write_async (doc,
+                       NULL,
+                       (GAsyncReadyCallback)file_save_write_cb,
+                       self);
 }
 
 static void
@@ -995,33 +1016,29 @@ save_action (GtkWidget *widget,
        file_save (self);
 }
 
-/* save_as helper */
 static void
-save_as_response_cb (GtkNativeDialog *dialog,
-               int resp,
-               gpointer user_data)
+save_as_write_to_file_cb (HexDocument *doc,
+               GAsyncResult *res,
+               GHexApplicationWindow *self)
 {
-       GHexApplicationWindow *self = GHEX_APPLICATION_WINDOW(user_data);
-       GtkFileChooser *chooser = GTK_FILE_CHOOSER(dialog);
-       HexDocument *doc;
-       GFile *gfile;
+       GFile *gfile = extra_user_data;
+       GError *local_error = NULL;
+       gboolean write_successful;
 
-       /* If user doesn't click Save, just bail out now. */
-       if (resp != GTK_RESPONSE_ACCEPT)
-               goto end;
+       write_successful = hex_document_write_finish (doc, res, &local_error);
 
-       /* Fetch doc. No need for sanity checks as this is just a helper. */
-       doc = hex_widget_get_document (ACTIVE_GH);
+       if (! write_successful)
+       {
+               GString *full_errmsg = g_string_new (
+                               _("There was an error saving the file to the path specified."));
 
-       gfile = gtk_file_chooser_get_file (chooser);
+               if (local_error)
+                       g_string_append_printf (full_errmsg, "\n\n%s", local_error->message);
 
-       if (! hex_document_write_to_file (doc, gfile))
-       {
-               display_error_dialog (GTK_WINDOW(self),
-                               _("There was an error saving the file to the path specified."
-                                       "\n\n"
-                                       "You may not have the required permissions."));
-               goto end;
+               display_error_dialog (GTK_WINDOW(self), full_errmsg->str);
+
+               g_string_free (full_errmsg, TRUE);
+               return;
        }
 
        if (hex_document_set_file (doc, gfile))
@@ -1034,8 +1051,34 @@ save_as_response_cb (GtkNativeDialog *dialog,
                display_error_dialog (GTK_WINDOW(self),
                                _("An unknown error has occurred in attempting to reload the "
                                        "file you have just saved."));
-               goto end;
        }
+}
+
+/* save_as helper */
+static void
+save_as_response_cb (GtkNativeDialog *dialog,
+               int resp,
+               gpointer user_data)
+{
+       GHexApplicationWindow *self = GHEX_APPLICATION_WINDOW(user_data);
+       GtkFileChooser *chooser = GTK_FILE_CHOOSER(dialog);
+       HexDocument *doc;
+       GFile *gfile;
+
+       /* If user doesn't click Save, just bail out now. */
+       if (resp != GTK_RESPONSE_ACCEPT)
+               goto end;
+
+       /* Fetch doc. No need for sanity checks as this is just a helper. */
+       doc = hex_widget_get_document (ACTIVE_GH);
+
+       extra_user_data = gfile = gtk_file_chooser_get_file (chooser);
+
+       hex_document_write_to_file_async (doc,
+                       gfile,
+                       NULL,
+                       (GAsyncReadyCallback)save_as_write_to_file_cb,
+                       self);
 
 end:
        gtk_native_dialog_destroy (GTK_NATIVE_DIALOG (dialog));
diff --git a/src/hex-buffer-mmap.c b/src/hex-buffer-mmap.c
index 9526c16..81b1c1f 100644
--- a/src/hex-buffer-mmap.c
+++ b/src/hex-buffer-mmap.c
@@ -178,6 +178,8 @@ set_error (HexBufferMmap *self, const char *blurb)
        }
        g_debug ("%s: %s", __func__, message);
 
+       g_clear_error (&self->error);
+
        g_set_error (&self->error,
                        HEX_BUFFER_MMAP_ERROR,
                        errno,
@@ -787,6 +789,7 @@ hex_buffer_mmap_write_to_file (HexBuffer *buf,
 
        hex_buffer_mmap_raw (self, &raw, 0, self->payload);
 
+       g_clear_error (&self->error);
        retval = g_file_replace_contents (file,
                /* const char* contents, */                     raw,    
                /* gsize length, */                                     self->payload,


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