Closed Bug 303572 Opened 19 years ago Closed 19 years ago

Need access to underlying RhinoException in rethrown error objects

Categories

(Rhino Graveyard :: Core, enhancement)

enhancement
Not set
normal

Tracking

(Not tracked)

RESOLVED FIXED

People

(Reporter: szegedia, Assigned: szegedia)

Details

Attachments

(1 file)

User-Agent:       Opera/8.01 (Windows NT 5.0; U; en)
Build Identifier: 

There's a situation where scripts catch and then selectively rethrow exceptions:

try
{
    ...
}
catch(e)
{
    if(e instanceof JavaException && e.javaException.class.name == "com.foo.
ValidationException")
    {
        ...
    }
    else
    {
        throw e;
    }
}

Now, when the thrown exception is some internal Rhino exception, i.e. TypeError, 
its stack trace will be lost after the throw -- the Java code invoking the 
script will ultimately get a JavaScriptException with the error object 
constructed in the catch clause. I'd suggest that similar to the nonstandard 
"javaException" field, the error object should also have an also nonstandard  
"rhinoException" field for these exceptions. Then the Java code invoking the 
script could do the following to ensure it logs the root cause exception 
regardless of how many times it is caught and rethrown in the script:

Throwable terminationCause;
try
{
    script.exec(...);
    terminationCause = null;
}
catch(JavaScriptException e)
{
    terminationCause = e;
    Object val = e.getValue();
    if(val instanceof Scriptable)
    {
        Object njo = ScriptableObject.getProperty(((Scriptable)val), 
                "rhinoException");
        if(njo instanceof NativeJavaObject)
        {
            val = njo;
        }
        else
        {
            njo = ScriptableObject.getProperty(((Scriptable)val), 
                "javaException");
            if(njo instanceof NativeJavaObject)
            {
                val = njo;
            }
        }
    }
    if(val instanceof NativeJavaObject)
    {
        Object t = ((NativeJavaObject)val).unwrap();
        while(t instanceof InvocationTargetException)
        {
            t = ((InvocationTargetException)t).getCause();
        }
        if(t instanceof Throwable)
        {
            terminationCause = (Throwable)t;
        }
    }
}
catch(Throwable t)
{
    terminationCause = t;
}
if(terminationCause != null)
{
    logger.error("Script failed", terminationCause);
}

I'll attach a patch

Reproducible: Always

Steps to Reproduce:
I've attached the patch that implements the feature. If nobody objects until
Monday, I'll commit it then.
Assignee: igor → szegedia
Committed the patch.
Status: NEW → RESOLVED
Closed: 19 years ago
Resolution: --- → FIXED
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: