[gjs] logError: format syntax error differently



commit af1e119d59b27c6b24df1659d8bc582124b9e2dd
Author: Giovanni Campagna <gcampagna src gnome org>
Date:   Tue May 28 22:15:24 2013 +0200

    logError: format syntax error differently
    
    The stack of a syntax error shows the importing module, not the
    imported one, so the information of which line has the error is
    lost.
    Also, syntax errors are definitely programmer errors, and should
    be criticals rather than warnings.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=722285

 gjs/context.cpp    |   13 ++++++++-
 gjs/jsapi-util.cpp |   81 ++++++++++++++++++++++++++++++++++++++++++---------
 gjs/jsapi-util.h   |    1 +
 3 files changed, 79 insertions(+), 16 deletions(-)
---
diff --git a/gjs/context.cpp b/gjs/context.cpp
index 2bafa91..66ee584 100644
--- a/gjs/context.cpp
+++ b/gjs/context.cpp
@@ -76,7 +76,7 @@ static const char *const_strings[] = {
     "__gjsKeepAlive", "__gjsPrivateNS",
     "gi", "versions", "overrides",
     "_init", "_new_internal", "new",
-    "message", "code", "stack", "fileName", "lineNumber"
+    "message", "code", "stack", "fileName", "lineNumber", "name",
 };
 
 G_STATIC_ASSERT(G_N_ELEMENTS(const_strings) == GJS_STRING_LAST);
@@ -606,6 +606,17 @@ gjs_context_eval(GjsContext   *js_context,
         goto out;
     }
 
+    gjs_debug(GJS_DEBUG_CONTEXT,
+              "Script evaluation succeeded");
+
+    if (gjs_log_exception(js_context->context)) {
+        g_set_error(error,
+                    GJS_ERROR,
+                    GJS_ERROR_FAILED,
+                    "Exception was set even though JS_EvaluateScript() returned true - did you gjs_throw() 
but not return false somewhere perhaps?");
+        goto out;
+    }
+
     if (exit_status_p) {
         if (JSVAL_IS_INT(retval)) {
             int code;
diff --git a/gjs/jsapi-util.cpp b/gjs/jsapi-util.cpp
index e3bae67..7c85503 100644
--- a/gjs/jsapi-util.cpp
+++ b/gjs/jsapi-util.cpp
@@ -487,10 +487,13 @@ gjs_log_exception_full(JSContext *context,
 {
     jsval stack;
     JSString *exc_str;
-    char *utf8_exception, *utf8_message, *utf8_stack;
+    char *utf8_exception, *utf8_message;
+    gboolean is_syntax;
 
     JS_BeginRequest(context);
 
+    is_syntax = FALSE;
+
     if (JSVAL_IS_OBJECT(exc) &&
         gjs_typecheck_boxed(context, JSVAL_TO_OBJECT(exc), NULL, G_TYPE_ERROR, FALSE)) {
         GError *gerror;
@@ -500,6 +503,18 @@ gjs_log_exception_full(JSContext *context,
                                          g_quark_to_string(gerror->domain),
                                          gerror->message);
     } else {
+        if (JSVAL_IS_OBJECT(exc)) {
+            jsval js_name;
+            char *utf8_name;
+
+            if (gjs_object_get_property_const(context, JSVAL_TO_OBJECT(exc),
+                                              GJS_STRING_NAME, &js_name) &&
+                JSVAL_IS_STRING(js_name) &&
+                gjs_string_to_utf8(context, js_name, &utf8_name)) {
+                is_syntax = strcmp("SyntaxError", utf8_name) == 0;
+            }
+        }
+
         exc_str = JS_ValueToString(context, exc);
         if (exc_str != NULL)
             gjs_string_to_utf8(context, STRING_TO_JSVAL(exc_str), &utf8_exception);
@@ -512,29 +527,65 @@ gjs_log_exception_full(JSContext *context,
     else
         utf8_message = NULL;
 
-    if (JSVAL_IS_OBJECT(exc) &&
+    /* We log syntax errors differently, because the stack for those includes
+       only the referencing module, but we want to print out the filename and
+       line number from the exception.
+    */
+
+    if (is_syntax) {
+        jsval js_lineNumber, js_fileName;
+        unsigned lineNumber;
+        char *utf8_fileName;
+
         gjs_object_get_property_const(context, JSVAL_TO_OBJECT(exc),
-                                      GJS_STRING_STACK, &stack) &&
-        JSVAL_IS_STRING(stack))
-        gjs_string_to_utf8(context, stack, &utf8_stack);
-    else
-        utf8_stack = NULL;
+                                      GJS_STRING_LINE_NUMBER, &js_lineNumber);
+        gjs_object_get_property_const(context, JSVAL_TO_OBJECT(exc),
+                                      GJS_STRING_FILENAME, &js_fileName);
 
-    if (utf8_message) {
-        if (utf8_stack)
-            g_warning("JS ERROR: %s: %s\n%s", utf8_message, utf8_exception, utf8_stack);
+        if (JSVAL_IS_STRING(js_fileName))
+            gjs_string_to_utf8(context, js_fileName, &utf8_fileName);
         else
-            g_warning("JS ERROR: %s: %s", utf8_message, utf8_exception);
+            utf8_fileName = g_strdup("unknown");
+
+        lineNumber = JSVAL_TO_INT(js_lineNumber);
+
+        if (utf8_message) {
+            g_critical("JS ERROR: %s: %s @ %s:%u", utf8_message, utf8_exception,
+                       utf8_fileName, lineNumber);
+        } else {
+            g_critical("JS ERROR: %s @ %s:%u", utf8_exception,
+                       utf8_fileName, lineNumber);
+        }
+
+        g_free(utf8_fileName);
     } else {
-        if (utf8_stack)
-            g_warning("JS ERROR: %s\n%s", utf8_exception, utf8_stack);
+        char *utf8_stack;
+
+        if (JSVAL_IS_OBJECT(exc) &&
+            gjs_object_get_property_const(context, JSVAL_TO_OBJECT(exc),
+                                          GJS_STRING_STACK, &stack) &&
+            JSVAL_IS_STRING(stack))
+            gjs_string_to_utf8(context, stack, &utf8_stack);
         else
-            g_warning("JS ERROR: %s", utf8_exception);
+            utf8_stack = NULL;
+
+        if (utf8_message) {
+            if (utf8_stack)
+                g_warning("JS ERROR: %s: %s\n%s", utf8_message, utf8_exception, utf8_stack);
+            else
+                g_warning("JS ERROR: %s: %s", utf8_message, utf8_exception);
+        } else {
+            if (utf8_stack)
+                g_warning("JS ERROR: %s\n%s", utf8_exception, utf8_stack);
+            else
+                g_warning("JS ERROR: %s", utf8_exception);
+        }
+
+        g_free(utf8_stack);
     }
 
     g_free(utf8_exception);
     g_free(utf8_message);
-    g_free(utf8_stack);
 
     JS_EndRequest(context);
 
diff --git a/gjs/jsapi-util.h b/gjs/jsapi-util.h
index f7cff11..e3a1aac 100644
--- a/gjs/jsapi-util.h
+++ b/gjs/jsapi-util.h
@@ -417,6 +417,7 @@ typedef enum {
   GJS_STRING_STACK,
   GJS_STRING_FILENAME,
   GJS_STRING_LINE_NUMBER,
+  GJS_STRING_NAME,
   GJS_STRING_LAST
 } GjsConstString;
 


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