[gjs] Improve error messages when we fail to look up a property



commit 6e9d4b916ae71fef0c76b12a445a5714d266ceaf
Author: Owen W. Taylor <otaylor redhat com>
Date:   Tue Mar 17 16:30:51 2009 -0400

    Improve error messages when we fail to look up a property
    
    Allow an "object description" to be passed gjs_object_require_object()
    to provide more informative error messages when a property is missing
    or undefined; it can often be very unclear which object is missing
    the property, especially if the object is an object literal like:
    
     { x: <some-typo'ed constant> }
---
 gi/arg.c         |    4 ++--
 gi/boxed.c       |    2 +-
 gi/object.c      |    2 +-
 gi/repo.c        |    8 ++++----
 gi/value.c       |    2 +-
 gjs/importer.c   |    6 +++---
 gjs/jsapi-util.c |   22 ++++++++++++++--------
 gjs/jsapi-util.h |    1 +
 8 files changed, 27 insertions(+), 20 deletions(-)

diff --git a/gi/arg.c b/gi/arg.c
index 2285002..13c3441 100644
--- a/gi/arg.c
+++ b/gi/arg.c
@@ -561,7 +561,7 @@ gjs_value_to_g_argument(JSContext      *context,
             guint32 length;
 
             if (!gjs_object_require_property(context,
-                                             JSVAL_TO_OBJECT(value),
+                                             JSVAL_TO_OBJECT(value), NULL,
                                              "length",
                                              &length_value) ||
                 !JS_ValueToECMAUint32(context, length_value, &length)) {
@@ -613,7 +613,7 @@ gjs_value_to_g_argument(JSContext      *context,
             guint32 length;
 
             if (!gjs_object_require_property(context,
-                                             JSVAL_TO_OBJECT(value),
+                                             JSVAL_TO_OBJECT(value), NULL,
                                              "length",
                                              &length_value) ||
                 !JS_ValueToECMAUint32(context, length_value, &length)) {
diff --git a/gi/boxed.c b/gi/boxed.c
index 89e19fe..c24f366 100644
--- a/gi/boxed.c
+++ b/gi/boxed.c
@@ -348,7 +348,7 @@ boxed_init_from_props(JSContext   *context,
             goto out;
         }
 
-        if (!gjs_object_require_property(context, props, name, &value))
+        if (!gjs_object_require_property(context, props, "property list", name, &value))
             goto out;
 
         if (!boxed_set_field_from_value(context, priv, field_info, value)) {
diff --git a/gi/object.c b/gi/object.c
index e4bd7a9..794f7d1 100644
--- a/gi/object.c
+++ b/gi/object.c
@@ -480,7 +480,7 @@ object_instance_props_to_g_parameters(JSContext   *context,
         if (!gjs_get_string_id(nameval, &name))
             goto free_array_and_fail;
 
-        if (!gjs_object_require_property(context, props, name, &value))
+        if (!gjs_object_require_property(context, props, "property list", name, &value))
             goto free_array_and_fail;
 
         switch (init_g_param_from_property(context, name,
diff --git a/gi/repo.c b/gi/repo.c
index 723c249..69114e1 100644
--- a/gi/repo.c
+++ b/gi/repo.c
@@ -65,7 +65,7 @@ resolve_namespace_object(JSContext  *context,
     const char *version;
 
     load_context = gjs_runtime_get_load_context(JS_GetRuntime(context));
-    if (!gjs_object_require_property(load_context, repo_obj, "versions", &versions_val) ||
+    if (!gjs_object_require_property(load_context, repo_obj, "GI repository object", "versions", &versions_val) ||
         !JSVAL_IS_OBJECT(versions_val)) {
         gjs_throw(context, "No 'versions' property in GI repository object");
         return NULL;
@@ -503,7 +503,7 @@ gjs_lookup_namespace_object_by_name(JSContext      *context,
     global = JS_GetGlobalObject(load_context);
 
     importer = JSVAL_VOID;
-    if (!gjs_object_require_property(load_context, global, "imports", &importer) ||
+    if (!gjs_object_require_property(load_context, global, "global object", "imports", &importer) ||
         !JSVAL_IS_OBJECT(importer)) {
         gjs_log_exception(load_context, NULL);
         gjs_throw(context, "No imports property in global object");
@@ -511,7 +511,7 @@ gjs_lookup_namespace_object_by_name(JSContext      *context,
     }
 
     girepository = JSVAL_VOID;
-    if (!gjs_object_require_property(load_context, JSVAL_TO_OBJECT(importer),
+    if (!gjs_object_require_property(load_context, JSVAL_TO_OBJECT(importer), "importer",
                                         "gi", &girepository) ||
         !JSVAL_IS_OBJECT(girepository)) {
         gjs_log_exception(load_context, NULL);
@@ -521,7 +521,7 @@ gjs_lookup_namespace_object_by_name(JSContext      *context,
 
     repo_obj = JSVAL_TO_OBJECT(girepository);
 
-    if (!gjs_object_require_property(context, repo_obj, ns, &ns_obj))
+    if (!gjs_object_require_property(context, repo_obj, "GI repository object", ns, &ns_obj))
         return NULL;
 
     if (!JSVAL_IS_OBJECT(ns_obj)) {
diff --git a/gi/value.c b/gi/value.c
index d318874..ce81c14 100644
--- a/gi/value.c
+++ b/gi/value.c
@@ -292,7 +292,7 @@ gjs_value_to_g_value(JSContext    *context,
             guint32 length;
 
             if (!gjs_object_require_property(context,
-                                             JSVAL_TO_OBJECT(value),
+                                             JSVAL_TO_OBJECT(value), NULL,
                                              "length",
                                              &length_value) ||
                 !JS_ValueToECMAUint32(context, length_value, &length)) {
diff --git a/gjs/importer.c b/gjs/importer.c
index c75f529..787ee46 100644
--- a/gjs/importer.c
+++ b/gjs/importer.c
@@ -470,7 +470,7 @@ do_import(JSContext  *context,
         return JS_FALSE;
     }
 
-    if (!gjs_object_require_property(context, obj, "searchPath", &search_path_val)) {
+    if (!gjs_object_require_property(context, obj, "importer", "searchPath", &search_path_val)) {
         return JS_FALSE;
     }
 
@@ -723,7 +723,7 @@ importer_new_enumerate(JSContext  *context,
             /* we are enumerating the prototype properties */
             return JS_TRUE;
 
-        if (!gjs_object_require_property(context, object, "searchPath", &search_path_val))
+        if (!gjs_object_require_property(context, object, "importer", "searchPath", &search_path_val))
             return JS_FALSE;
 
         if (!JSVAL_IS_OBJECT(search_path_val)) {
@@ -1191,7 +1191,7 @@ gjs_define_root_importer(JSContext   *context,
     load_context = gjs_runtime_get_load_context(JS_GetRuntime(context));
 
     if (!gjs_object_require_property(load_context,
-                                     JS_GetGlobalObject(load_context),
+                                     JS_GetGlobalObject(load_context), "global object",
                                      "imports", &value) ||
         !JSVAL_IS_OBJECT(value)) {
         gjs_debug(GJS_DEBUG_IMPORTER, "Root importer did not exist, couldn't get from load context; must create it");
diff --git a/gjs/jsapi-util.c b/gjs/jsapi-util.c
index 20d3379..f42a95b 100644
--- a/gjs/jsapi-util.c
+++ b/gjs/jsapi-util.c
@@ -253,9 +253,10 @@ gjs_object_get_property(JSContext  *context,
  */
 gboolean
 gjs_object_require_property(JSContext       *context,
-                               JSObject        *obj,
-                               const char      *property_name,
-                               jsval           *value_p)
+                            JSObject        *obj,
+                            const char      *obj_description,
+                            const char      *property_name,
+                            jsval           *value_p)
 {
     jsval value;
 
@@ -272,9 +273,14 @@ gjs_object_require_property(JSContext       *context,
         /* remember gjs_throw() is a no-op if JS_GetProperty()
          * already set an exception
          */
-        gjs_throw(context,
-                     "No property '%s' in object %p (or its value was undefined)",
-                     property_name, obj);
+        if (obj_description)
+            gjs_throw(context,
+                      "No property '%s' in %s (or its value was undefined)",
+                      property_name, obj_description);
+        else
+            gjs_throw(context,
+                      "No property '%s' in object %p (or its value was undefined)",
+                      property_name, obj);
         return FALSE;
     }
 }
@@ -326,7 +332,7 @@ gjs_init_class_dynamic(JSContext      *context,
 
         g_free(private_name); /* don't need it anymore */
 
-        if (!gjs_object_require_property(context, JSVAL_TO_OBJECT(value),
+        if (!gjs_object_require_property(context, JSVAL_TO_OBJECT(value), NULL,
                                             "prototype", &proto_val) ||
             !JSVAL_IS_OBJECT(proto_val)) {
             gjs_throw(context, "prototype was not defined or not an object?");
@@ -364,7 +370,7 @@ gjs_init_class_dynamic(JSContext      *context,
         /* Retrieve the property again so we can define it in
          * in_object
          */
-        if (!gjs_object_require_property(context, JS_GetGlobalObject(context),
+        if (!gjs_object_require_property(context, JS_GetGlobalObject(context), NULL,
                                             class_copy->base.name, &value))
             return NULL;
     }
diff --git a/gjs/jsapi-util.h b/gjs/jsapi-util.h
index 5c0494b..2f12a75 100644
--- a/gjs/jsapi-util.h
+++ b/gjs/jsapi-util.h
@@ -114,6 +114,7 @@ gboolean    gjs_object_get_property          (JSContext       *context,
                                               jsval           *value_p);
 gboolean    gjs_object_require_property      (JSContext       *context,
                                               JSObject        *obj,
+                                              const char      *obj_description,
                                               const char      *property_name,
                                               jsval           *value_p);
 JSObject *  gjs_init_class_dynamic           (JSContext       *context,



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