Better JS error message

Harri Porten porten at froglogic.com
Sat Nov 29 19:44:16 CET 2003


Hi,

tired of those "Undefined value" type errors ? Inspired by the recent
Node::throwError() enhancement patch from JavaScriptCore I improved upon
the aforementioned message.

The most common cause for this error is an expression of the form a.b
where a is invalid due to an earlier evaluation failing. With the attached
patch the error is caught a bit earlier to provide a more meaningful error
message.

What do you think ? Should this be applied as is ? Maybe for a["b"], too ?
As the check is redundant I could also imagine putting it within #ifndef
NDEBUG.

Harri.
-------------- next part --------------
? JSArraySortPatch.txt
? diff
? diff.txt
? error.diff
? grammar.diff
? mozilla_failures.txt
Index: nodes.cpp
===================================================================
RCS file: /home/kde/kdelibs/kjs/nodes.cpp,v
retrieving revision 1.165
diff -u -3 -p -r1.165 nodes.cpp
--- nodes.cpp	18 Nov 2003 09:31:08 -0000	1.165
+++ nodes.cpp	29 Nov 2003 18:35:58 -0000
@@ -162,7 +162,8 @@ Value Node::throwError(ExecState *exec, 
   return err;
 }
 
-Value Node::throwError(ExecState *exec, ErrorType e, const char *msg, Value v, Node *expr) const
+Value Node::throwError(ExecState *exec, ErrorType e, const char *msg,
+                       const Value &v, const Node *expr) const
 {
   char *vStr = strdup(v.toString(exec).ascii());
   char *exprStr = strdup(expr->toCode().ascii());
@@ -638,6 +639,13 @@ Reference AccessorNode2::evaluateReferen
   Value v = expr->evaluate(exec);
   assert(v.isValid());
   KJS_CHECKEXCEPTIONREFERENCE
+  // catch errors before being caught in toObject(). better error message.
+  if (v.isA(UndefinedType) || v.isA(NullType)) {
+    UString s = "Attempted to access '" + ident.ustring() +
+                "' property on %s object (result of expression %s)";
+        (void)throwError(exec, TypeError, s.cstring().c_str(), v, this);
+    return Reference::makeValueReference(Undefined());
+  }
   Object o = v.toObject(exec);
   return Reference(o, ident);
 }
Index: nodes.h
===================================================================
RCS file: /home/kde/kdelibs/kjs/nodes.h,v
retrieving revision 1.86
diff -u -3 -p -r1.86 nodes.h
--- nodes.h	5 Nov 2003 16:06:50 -0000	1.86
+++ nodes.h	29 Nov 2003 18:35:58 -0000
@@ -113,7 +113,8 @@ namespace KJS {
 #endif
   protected:
     Value throwError(ExecState *exec, ErrorType e, const char *msg) const;
-    Value throwError(ExecState *exec, ErrorType e, const char *msg, Value v, Node *expr) const;
+    Value throwError(ExecState *exec, ErrorType e, const char *msg,
+                     const Value &v, const Node *expr) const;
     Value throwError(ExecState *exec, ErrorType e, const char *msg, Identifier label) const;
     int line;
     unsigned int refcount;


More information about the Khtml-devel mailing list