[gjs] util: improve logging of unrecognized GErrors



commit 61acb494e7d0676ef80f6e4285213f0a9d669a4a
Author: Giovanni Campagna <gcampagn redhat com>
Date:   Wed Sep 25 15:29:01 2013 +0200

    util: improve logging of unrecognized GErrors
    
    If a GError is not from a known domain, it will be marshalled as
    a regular boxed type, and thus will not have the special toString()
    that logError() relies on for type and message.
    Recognize that case and extract the information from the GError
    structure directly.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=708749

 gjs/jsapi-util.c                          |   21 +++++++--
 installed-tests/js/testEverythingBasic.js |   64 +++++++++++++++++++++++++++++
 2 files changed, 80 insertions(+), 5 deletions(-)
---
diff --git a/gjs/jsapi-util.c b/gjs/jsapi-util.c
index 62b0510..15b42ac 100644
--- a/gjs/jsapi-util.c
+++ b/gjs/jsapi-util.c
@@ -32,6 +32,7 @@
 #include "compat.h"
 #include "jsapi-private.h"
 #include "runtime.h"
+#include <gi/boxed.h>
 
 #include <string.h>
 #include <math.h>
@@ -460,11 +461,21 @@ gjs_log_exception_full(JSContext *context,
 
     JS_BeginRequest(context);
 
-    exc_str = JS_ValueToString(context, exc);
-    if (exc_str != NULL)
-        gjs_string_to_utf8(context, STRING_TO_JSVAL(exc_str), &utf8_exception);
-    else
-        utf8_exception = NULL;
+    if (JSVAL_IS_OBJECT(exc) &&
+        gjs_typecheck_boxed(context, JSVAL_TO_OBJECT(exc), NULL, G_TYPE_ERROR, FALSE)) {
+        GError *gerror;
+
+        gerror = gjs_c_struct_from_boxed(context, JSVAL_TO_OBJECT(exc));
+        utf8_exception = g_strdup_printf("GLib.Error %s: %s",
+                                         g_quark_to_string(gerror->domain),
+                                         gerror->message);
+    } else {
+        exc_str = JS_ValueToString(context, exc);
+        if (exc_str != NULL)
+            gjs_string_to_utf8(context, STRING_TO_JSVAL(exc_str), &utf8_exception);
+        else
+            utf8_exception = NULL;
+    }
 
     if (message != NULL)
         gjs_string_to_utf8(context, STRING_TO_JSVAL(message), &utf8_message);
diff --git a/installed-tests/js/testEverythingBasic.js b/installed-tests/js/testEverythingBasic.js
index 3b715f8..c1f66ed 100644
--- a/installed-tests/js/testEverythingBasic.js
+++ b/installed-tests/js/testEverythingBasic.js
@@ -573,6 +573,70 @@ function testGError() {
     }
 }
 
+function testGErrorMessages() {
+    GLib.test_expect_message('Gjs', GLib.LogLevelFlags.LEVEL_WARNING,
+                             'JS ERROR: Gio.IOErrorEnum: *');
+    try {
+       let file = Gio.file_new_for_path("\\/,.^!@&$_don't exist");
+        file.read(null);
+    } catch(e) {
+       logError(e);
+    }
+
+    GLib.test_expect_message('Gjs', GLib.LogLevelFlags.LEVEL_WARNING,
+                            'JS ERROR: Gio.IOErrorEnum: a message\ntestGErrorMessages *');
+    try {
+       throw new Gio.IOErrorEnum({ message: 'a message', code: 0 });
+    } catch(e) {
+       logError(e);
+    }
+
+    GLib.test_expect_message('Gjs', GLib.LogLevelFlags.LEVEL_WARNING,
+                            'JS ERROR: Gio.IOErrorEnum: a message\ntestGErrorMessages *');
+    logError(new Gio.IOErrorEnum({ message: 'a message', code: 0 }));
+
+    // No stack for GLib.Error constructor
+    GLib.test_expect_message('Gjs', GLib.LogLevelFlags.LEVEL_WARNING,
+                            'JS ERROR: Gio.IOErrorEnum: a message');
+    logError(new GLib.Error(Gio.IOErrorEnum, 0, 'a message'));
+
+    GLib.test_expect_message('Gjs', GLib.LogLevelFlags.LEVEL_WARNING,
+                            'JS ERROR: GLib.Error my-error: a message');
+    logError(new GLib.Error(GLib.quark_from_string('my-error'), 0, 'a message'));
+
+    // Now with prefix
+
+    GLib.test_expect_message('Gjs', GLib.LogLevelFlags.LEVEL_WARNING,
+                             'JS ERROR: prefix: Gio.IOErrorEnum: *');
+    try {
+       let file = Gio.file_new_for_path("\\/,.^!@&$_don't exist");
+        file.read(null);
+    } catch(e) {
+       logError(e, 'prefix');
+    }
+
+    GLib.test_expect_message('Gjs', GLib.LogLevelFlags.LEVEL_WARNING,
+                            'JS ERROR: prefix: Gio.IOErrorEnum: a message\ntestGErrorMessages *');
+    try {
+       throw new Gio.IOErrorEnum({ message: 'a message', code: 0 });
+    } catch(e) {
+       logError(e, 'prefix');
+    }
+
+    GLib.test_expect_message('Gjs', GLib.LogLevelFlags.LEVEL_WARNING,
+                            'JS ERROR: prefix: Gio.IOErrorEnum: a message\ntestGErrorMessages *');
+    logError(new Gio.IOErrorEnum({ message: 'a message', code: 0 }), 'prefix');
+
+    // No stack for GLib.Error constructor
+    GLib.test_expect_message('Gjs', GLib.LogLevelFlags.LEVEL_WARNING,
+                            'JS ERROR: prefix: Gio.IOErrorEnum: a message');
+    logError(new GLib.Error(Gio.IOErrorEnum, 0, 'a message'), 'prefix');
+
+    GLib.test_expect_message('Gjs', GLib.LogLevelFlags.LEVEL_WARNING,
+                            'JS ERROR: prefix: GLib.Error my-error: a message');
+    logError(new GLib.Error(GLib.quark_from_string('my-error'), 0, 'a message'), 'prefix');
+}
+
 function testWrongClassGObject() {
     /* Function calls */
     // Everything.func_obj_null_in expects a Everything.TestObj


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