Revision: 3764
          http://vexi.svn.sourceforge.net/vexi/?rev=3764&view=rev
Author:   mkpg2
Date:     2009-12-08 21:04:55 +0000 (Tue, 08 Dec 2009)

Log Message:
-----------
Feature. Support exception chaining in js, through cause property. 

Modified Paths:
--------------
    trunk/core/org.ibex.js/src/org/ibex/js/Constants.java
    trunk/core/org.ibex.js/src/org/ibex/js/JSExn.jpp
    trunk/core/org.ibex.js/src/org/vexi/js/script.js
    trunk/core/org.ibex.js/src_dev/dev/RunScript.java

Added Paths:
-----------
    trunk/core/org.ibex.js/src_dev/dev/poke.js

Modified: trunk/core/org.ibex.js/src/org/ibex/js/Constants.java
===================================================================
--- trunk/core/org.ibex.js/src/org/ibex/js/Constants.java       2009-12-08 
02:47:35 UTC (rev 3763)
+++ trunk/core/org.ibex.js/src/org/ibex/js/Constants.java       2009-12-08 
21:04:55 UTC (rev 3764)
@@ -6,6 +6,7 @@
     static final JS SC_array = JSU.S("array",true);
        static final JS SC_boolean = JSU.S("boolean",true);
        static final JS SC_callee = JSU.S("callee",true);
+       static final JS SC_cause = JSU.S("cause",true);
        static final JS SC_code = JSU.S("code",true);
        static final JS SC_date = JSU.S("date",true);
        static final JS SC_Elements = JSU.S("Elements",true);

Modified: trunk/core/org.ibex.js/src/org/ibex/js/JSExn.jpp
===================================================================
--- trunk/core/org.ibex.js/src/org/ibex/js/JSExn.jpp    2009-12-08 02:47:35 UTC 
(rev 3763)
+++ trunk/core/org.ibex.js/src/org/ibex/js/JSExn.jpp    2009-12-08 21:04:55 UTC 
(rev 3764)
@@ -61,6 +61,16 @@
                cx = cx.old;
         }
     }
+    
+    public Throwable getCause(){
+        Throwable r = super.getCause();
+        if(r!=null) return r;
+        
+        JS c = getObject().getSafe(SC_cause);
+        if(c!=null) return ((ExnJSObj)c).getJSExn();
+        return null;
+    }
+    
     public void printStackTrace() { printStackTrace(System.err); }
     public void printStackTrace(PrintStream ps) { printStackTrace(new 
PrintWriter(ps)); }
     public void printStackTrace(PrintWriter pw) { 
@@ -85,10 +95,15 @@
         // If there is a cause it is because it generally/should mean we 
didn't know
         // how to handle the exception. So we want to print its stack trace. 
The actual
         // java stack trace of the jsexn only gets printed when debug log 
level is set.
-        if(level<=Logger.DEBUG) super.printStackTrace(pw);
-        else if(getCause()!=null){
-               if(level<=Logger.ERROR && js.getSafe(SC_type).equals(SC_error)) 
{
-                       getCause().printStackTrace(pw);
+        if(level<=Logger.DEBUG) 
+            super.printStackTrace(pw);
+        else {
+            Throwable ourCause = getCause();
+            if (ourCause != null){
+                if(ourCause instanceof JSExn || 
+                   (level<=Logger.ERROR && 
SC_error.equals(js.getSafe(SC_type)) )){
+                    ourCause.printStackTrace(pw);
+                }
                }
         }
         pw.flush();
@@ -138,6 +153,22 @@
                        }
                        return r;
                }
+            /* 
+             * Property used for exception chaining. If the reason to
+             * throw the exception is not being able to recover when 
+             * handling another exception then this can be set so that
+             * complete information about the error can be communicated
+             * up out of the call stack.
+             * 
+             * @type(Exception)
+             * */
+               case "cause": {
+                   Throwable e = getCause();
+                   if(e instanceof JSExn){
+                       return ((JSExn)e).getObject();
+                   }
+                   return null;
+               }
                /* 
                 * The message describing the reason the exception was thrown. 
                 * 
@@ -153,6 +184,19 @@
                //#end
                return super.get(key);
        }
+       
+       public void put(JS key, JS val) throws JSExn{
+            //#switch(JSU.toString(key))
+            case "cause": {
+                if(!(val instanceof ExnJSObj)){
+                    throw new JSExn("Not a valid cause: "+ key);
+                }
+                super.put(key,val);
+            }
+            //#end 
+            super.put(key,val);
+        }
+       
                public String getMessage(){
                        try{
                                return JSU.toString(js.get(SC_message));

Modified: trunk/core/org.ibex.js/src/org/vexi/js/script.js
===================================================================
--- trunk/core/org.ibex.js/src/org/vexi/js/script.js    2009-12-08 02:47:35 UTC 
(rev 3763)
+++ trunk/core/org.ibex.js/src/org/vexi/js/script.js    2009-12-08 21:04:55 UTC 
(rev 3764)
@@ -131,6 +131,11 @@
             var line = backtrace[i];
             r[i+1] = " at "+line;
         }
+        // chained exceptions
+        var cause = e.cause;
+        if(cause and typeof(cause)=="exception"){
+               r.push(formatException(cause));
+        } 
         return r.join("\n");
     }
     

Modified: trunk/core/org.ibex.js/src_dev/dev/RunScript.java
===================================================================
--- trunk/core/org.ibex.js/src_dev/dev/RunScript.java   2009-12-08 02:47:35 UTC 
(rev 3763)
+++ trunk/core/org.ibex.js/src_dev/dev/RunScript.java   2009-12-08 21:04:55 UTC 
(rev 3764)
@@ -6,6 +6,6 @@
 public class RunScript {
        static public void main(String[] args) throws Exception {
                Fountain dir = JSTestUtil.getResourceFountain(RunScript.class, 
".js");
-               RunJS.runJSFile(dir, "robert.js");
+               RunJS.runJSFile(dir, "poke.js");
        }
 }

Added: trunk/core/org.ibex.js/src_dev/dev/poke.js
===================================================================
--- trunk/core/org.ibex.js/src_dev/dev/poke.js                          (rev 0)
+++ trunk/core/org.ibex.js/src_dev/dev/poke.js  2009-12-08 21:04:55 UTC (rev 
3764)
@@ -0,0 +1,25 @@
+
+var f = function(){
+       throw "!";
+};
+
+var g = function(){
+       try{
+           f();
+       }catch(e){
+               var e2 = new sys.js.Exception();
+               e2.message = "!!";
+               e2.cause = e;
+               throw e2;
+       }
+};
+
+
+try{
+       g();
+}catch(e){
+       sys.trace(e);
+       sys.log.warn(e);
+}
+
+g();


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
Return on Information:
Google Enterprise Search pays you back
Get the facts.
http://p.sf.net/sfu/google-dev2dev
_______________________________________________
Vexi-svn mailing list
Vexi-svn@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/vexi-svn

Reply via email to