Browse Source

try to unwrap ScriptException - unfortunately it doesn't seem to work - PR 47509

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@793540 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 16 years ago
parent
commit
3499194926
2 changed files with 37 additions and 2 deletions
  1. +24
    -2
      src/main/org/apache/tools/ant/util/optional/JavaxScriptRunner.java
  2. +13
    -0
      src/tests/antunit/taskdefs/optional/script/scriptdef-test.xml

+ 24
- 2
src/main/org/apache/tools/ant/util/optional/JavaxScriptRunner.java View File

@@ -105,11 +105,16 @@ public class JavaxScriptRunner extends ScriptRunnerBase {
return engine.invoke("eval", String.class, getScript());
} catch (BuildException be) {
//catch and rethrow build exceptions
throw be;

// this may be a BuildException wrapping a ScriptException
// deeply wrapping yet another BuildException - for
// example because of self.fail() - see
// https://issues.apache.org/bugzilla/show_bug.cgi?id=47509
throw unwrap(be);
} catch (Exception be) {
//any other exception? Get its cause
Throwable t = be;
Throwable te = (Throwable) ReflectUtil.invoke(be, "getCause");
Throwable te = be.getCause();
if (te != null) {
if (te instanceof BuildException) {
throw (BuildException) te;
@@ -140,4 +145,21 @@ public class JavaxScriptRunner extends ScriptRunnerBase {
}
return ret;
}

/**
* Traverse a Throwable's cause(s) and return the BuildException
* most deeply nested into it - if any.
*/
private static BuildException unwrap(Throwable t) {
BuildException deepest =
t instanceof BuildException ? (BuildException) t : null;
Throwable current = t;
while (current.getCause() != null) {
current = current.getCause();
if (current instanceof BuildException) {
deepest = (BuildException) current;
}
}
return deepest;
}
}

+ 13
- 0
src/tests/antunit/taskdefs/optional/script/scriptdef-test.xml View File

@@ -109,4 +109,17 @@
<scripttest/>
<assertPropSet name="property2" />
</target>

<target name="testExceptionNesting"
description="https://issues.apache.org/bugzilla/show_bug.cgi?id=47509"
if="prereqs-ok">
<scriptdef name="quickfail" language="javascript">
<![CDATA[
self.fail("I failed!");
]]>
</scriptdef>
<au:expectfailure message="I failed!">
<quickfail/>
</au:expectfailure>
</target>
</project>

Loading…
Cancel
Save