diff src/utils.cpp @ 120:856ca7a139e4

Pymonkey errors raised as a result of JS exceptions now contain both the string representation and the original Python-wrapped JS object that was thrown (i.e., the exception).
author Atul Varma <varmaa@toolness.com>
date Mon, 17 Aug 2009 23:42:20 -0700
parents ac8ca0ee7760
children 08a012e96f62
line wrap: on
line diff
--- a/src/utils.cpp	Mon Aug 17 22:36:42 2009 -0700
+++ b/src/utils.cpp	Mon Aug 17 23:42:20 2009 -0700
@@ -233,25 +233,35 @@
 
   jsval val;
   if (JS_GetPendingException(context->cx, &val)) {
+    PyObject *pyStr = NULL;
+    PyObject *tuple = NULL;
     PyObject *obj = PYM_jsvalToPyObject(context, val);
-    if (obj) {
-      PyErr_SetObject(PYM_error, obj);
-      Py_DECREF(obj);
-    } else {
+    if (obj == NULL) {
       PyErr_Clear();
+      obj = Py_None;
+      Py_INCREF(obj);
+    }
 
-      JSString *str = NULL;
+    JSString *str = NULL;
+
+    Py_BEGIN_ALLOW_THREADS;
+    str = JS_ValueToString(context->cx, val);
+    Py_END_ALLOW_THREADS;
 
-      Py_BEGIN_ALLOW_THREADS;
-      str = JS_ValueToString(context->cx, val);
-      Py_END_ALLOW_THREADS;
+    if (str != NULL)
+      pyStr = PYM_jsvalToPyObject(context, STRING_TO_JSVAL(str));
+    else
+      // TODO: Is there an exception in JS-land we should clear?
+      pyStr = PyString_FromString("<string conversion failed>");
 
-      if (str != NULL) {
-        const char *chars = JS_GetStringBytes(str);
-        PyErr_SetString(PYM_error, chars);
-      } else
-        PyErr_SetString(PYM_error, "JS exception occurred");
+    if (pyStr) {
+      tuple = Py_BuildValue("(OO)", obj, pyStr);
+      if (tuple)
+        PyErr_SetObject(PYM_error, tuple);
     }
+    Py_DECREF(obj);
+    Py_XDECREF(pyStr);
+    Py_XDECREF(tuple);
   } else
     PyErr_SetString(PYM_error, "JS_GetPendingException() failed");
 }