[gnome-software] Add gs_plugin_error_add_unique_id()



commit 80ebce027bb225617bde453f8d27a3b2af7e6c5b
Author: Richard Hughes <richard hughsie com>
Date:   Wed Sep 7 17:35:43 2016 +0100

    Add gs_plugin_error_add_unique_id()
    
    This allows us to prepend the unique-id onto the error message.

 src/gs-plugin-private.h |    1 +
 src/gs-plugin.c         |   44 ++++++++++++++++++++++++++++++++++++++++++++
 src/gs-plugin.h         |    2 ++
 src/gs-self-test.c      |   14 ++++++++++++++
 4 files changed, 61 insertions(+), 0 deletions(-)
---
diff --git a/src/gs-plugin-private.h b/src/gs-plugin-private.h
index ba0cada..bf3b9b4 100644
--- a/src/gs-plugin-private.h
+++ b/src/gs-plugin-private.h
@@ -117,6 +117,7 @@ GsPlugin    *gs_plugin_create                       (const gchar    *filename,
 const gchar    *gs_plugin_error_to_string              (GsPluginError   error);
 const gchar    *gs_plugin_action_to_string             (GsPluginAction  action);
 
+void            gs_plugin_error_strip_unique_id        (GError         *error);
 void            gs_plugin_action_start                 (GsPlugin       *plugin,
                                                         gboolean        exclusive);
 void            gs_plugin_action_stop                  (GsPlugin       *plugin);
diff --git a/src/gs-plugin.c b/src/gs-plugin.c
index 6a2f6f1..f83c64c 100644
--- a/src/gs-plugin.c
+++ b/src/gs-plugin.c
@@ -1398,6 +1398,50 @@ gs_plugin_action_to_string (GsPluginAction action)
        return NULL;
 }
 
+/**
+ * gs_plugin_error_add_unique_id:
+ * @error: a #GError
+ * @app: a #GsApp
+ *
+ * Adds a unique ID prefix to the error.
+ *
+ * Since: 3.22
+ **/
+void
+gs_plugin_error_add_unique_id (GError **error, GsApp *app)
+{
+       if (error == NULL || *error == NULL)
+               return;
+       g_prefix_error (error, "[%s] ", gs_app_get_unique_id (app));
+}
+
+/**
+ * gs_plugin_error_strip_unique_id:
+ * @error: a #GError
+ * @app: a #GsApp
+ *
+ * Removes a possible unique ID prefix from the error.
+ *
+ * Since: 3.22
+ **/
+void
+gs_plugin_error_strip_unique_id (GError *error)
+{
+       gchar *str;
+       if (error == NULL)
+               return;
+       if (!g_str_has_prefix (error->message, "["))
+               return;
+       str = g_strstr_len (error->message, -1, " ");
+       if (str == NULL)
+               return;
+
+       /* gahh, my eyes are bleeding */
+       str = g_strdup (str + 1);
+       g_free (error->message);
+       error->message = str;
+}
+
 static void
 gs_plugin_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
 {
diff --git a/src/gs-plugin.h b/src/gs-plugin.h
index 7cceeda..43c2d0b 100644
--- a/src/gs-plugin.h
+++ b/src/gs-plugin.h
@@ -303,6 +303,8 @@ gboolean     gs_plugin_app_launch                   (GsPlugin       *plugin,
 void            gs_plugin_updates_changed              (GsPlugin       *plugin);
 void            gs_plugin_reload                       (GsPlugin       *plugin);
 const gchar    *gs_plugin_status_to_string             (GsPluginStatus  status);
+void            gs_plugin_error_add_unique_id          (GError         **error,
+                                                        GsApp          *app);
 
 G_END_DECLS
 
diff --git a/src/gs-self-test.c b/src/gs-self-test.c
index cc37442..1e92c0a 100644
--- a/src/gs-self-test.c
+++ b/src/gs-self-test.c
@@ -92,8 +92,22 @@ static void
 gs_plugin_error_func (void)
 {
        guint i;
+       g_autoptr(GError) error = NULL;
+       g_autoptr(GsApp) app = gs_app_new ("gimp.desktop");
+
        for (i = 0; i < GS_PLUGIN_ERROR_LAST; i++)
                g_assert (gs_plugin_error_to_string (i) != NULL);
+
+       gs_plugin_error_add_unique_id (&error, app);
+       g_set_error (&error,
+                    GS_PLUGIN_ERROR,
+                    GS_PLUGIN_ERROR_DOWNLOAD_FAILED,
+                    "failed");
+       g_assert_cmpstr (error->message, ==, "failed");
+       gs_plugin_error_add_unique_id (&error, app);
+       g_assert_cmpstr (error->message, ==, "[*/*/*/*/gimp.desktop/*] failed");
+       gs_plugin_error_strip_unique_id (error);
+       g_assert_cmpstr (error->message, ==, "failed");
 }
 
 static void


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