|
[HtmlUnit] CVS Commit: gargoylesoftware/htmlunit: include reference to htm: msg#00019java.htmlunit.devel
Log Message: ----------- include reference to html page in ScriptException Modified Files: -------------- htmlunit/src/java/com/gargoylesoftware/htmlunit/javascript: JavaScriptEngine.java (http://cvs.sourceforge.net/viewcvs.py/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit/javascript/JavaScriptEngine.java) SimpleScriptable.java (http://cvs.sourceforge.net/viewcvs.py/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit/javascript/SimpleScriptable.java) htmlunit/src/test/java/com/gargoylesoftware/htmlunit: ScriptExceptionTest.java (http://cvs.sourceforge.net/viewcvs.py/htmlunit/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/ScriptExceptionTest.java) htmlunit/src/java/com/gargoylesoftware/htmlunit/html: HtmlPage.java (http://cvs.sourceforge.net/viewcvs.py/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit/html/HtmlPage.java) htmlunit/src/java/com/gargoylesoftware/htmlunit: ScriptException.java (http://cvs.sourceforge.net/viewcvs.py/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit/ScriptException.java) Revision Data ------------- Index: HtmlPage.java =================================================================== RCS file: /cvsroot/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit/html/HtmlPage.java,v retrieving revision 1.133 retrieving revision 1.134 diff -Lsrc/java/com/gargoylesoftware/htmlunit/html/HtmlPage.java -Lsrc/java/com/gargoylesoftware/htmlunit/html/HtmlPage.java -u -d -r1.133 -r1.134 --- src/java/com/gargoylesoftware/htmlunit/html/HtmlPage.java +++ src/java/com/gargoylesoftware/htmlunit/html/HtmlPage.java @@ -763,7 +763,7 @@ catch( final MalformedURLException e ) { getLog().error("Unable to build url for script src tag [" + srcAttribute + "]"); if (getWebClient().isThrowExceptionOnScriptError()) { - throw new ScriptException(e); + throw new ScriptException(this, e); } return; } @@ -842,7 +842,7 @@ catch( final Exception e ) { getLog().error("Error loading javascript from [" + url.toExternalForm() + "]: ", e); if (getWebClient().isThrowExceptionOnScriptError()) { - throw new ScriptException(e); + throw new ScriptException(this, e); } } return ""; Index: ScriptExceptionTest.java =================================================================== RCS file: /cvsroot/htmlunit/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/ScriptExceptionTest.java,v retrieving revision 1.1 retrieving revision 1.2 diff -Lsrc/test/java/com/gargoylesoftware/htmlunit/ScriptExceptionTest.java -Lsrc/test/java/com/gargoylesoftware/htmlunit/ScriptExceptionTest.java -u -d -r1.1 -r1.2 --- src/test/java/com/gargoylesoftware/htmlunit/ScriptExceptionTest.java +++ src/test/java/com/gargoylesoftware/htmlunit/ScriptExceptionTest.java @@ -37,9 +37,10 @@ */ package com.gargoylesoftware.htmlunit; +import com.gargoylesoftware.htmlunit.html.HtmlPage; /** - * Tests for {@link ScriptException}.. + * Tests for {@link ScriptException}. * * @version $Revision$ * @author Marc Guillemot @@ -48,19 +49,23 @@ /** * Create an instance. - * @param name The name of the test. + * + * @param name + * The name of the test. */ public ScriptExceptionTest(final String name) { super(name); } /** - * @throws Exception if the test fails + * @throws Exception + * if the test fails */ public void testConstructor() throws Exception { final String message = "bla bla"; final Throwable t = new RuntimeException(message); - final ScriptException exception = new ScriptException(t); + final HtmlPage page = loadPage("<html></html>"); + final ScriptException exception = new ScriptException(page, t); assertEquals(t, exception.getCause()); assertEquals(message, exception.getMessage()); @@ -68,13 +73,33 @@ /** * To remove when deprecated method have been removed + * * @deprecated - * @throws Exception if the test fails + * @throws Exception + * if the test fails */ public void testDeprecated() throws Exception { final Throwable t = new RuntimeException(); - final ScriptException exception = new ScriptException(t); + final HtmlPage page = loadPage("<html></html>"); + final ScriptException exception = new ScriptException(page, t); assertEquals(t, exception.getEnclosedException()); } + + /** + * Test access to the page where the exception occured from the exception + * + * @throws Exception + * if the test fails + */ + public void testGetPage() throws Exception { + final String html = "<html><script>notExisting()</script></html>"; + + try { + loadPage(html); + } + catch (final ScriptException e) { + assertEquals(URL_GARGOYLE, e.getPage().getWebResponse().getUrl()); + } + } } Index: SimpleScriptable.java =================================================================== RCS file: /cvsroot/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit/javascript/SimpleScriptable.java,v retrieving revision 1.71 retrieving revision 1.72 diff -Lsrc/java/com/gargoylesoftware/htmlunit/javascript/SimpleScriptable.java -Lsrc/java/com/gargoylesoftware/htmlunit/javascript/SimpleScriptable.java -u -d -r1.71 -r1.72 --- src/java/com/gargoylesoftware/htmlunit/javascript/SimpleScriptable.java +++ src/java/com/gargoylesoftware/htmlunit/javascript/SimpleScriptable.java @@ -37,7 +37,6 @@ */ package com.gargoylesoftware.htmlunit.javascript; -import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import org.apache.commons.collections.Transformer; @@ -50,7 +49,6 @@ import com.gargoylesoftware.htmlunit.Assert; import com.gargoylesoftware.htmlunit.BrowserVersion; -import com.gargoylesoftware.htmlunit.ScriptException; import com.gargoylesoftware.htmlunit.WebWindow; import com.gargoylesoftware.htmlunit.html.DomNode; import com.gargoylesoftware.htmlunit.html.HtmlElement; @@ -116,7 +114,7 @@ return scriptable; } catch( final Exception e ) { - throw new ScriptException(e); + throw Context.throwAsScriptRuntimeEx(e); } finally { Context.exit(); @@ -244,7 +242,7 @@ result = propertyMethod.invoke(this, new Object[0]); } catch (final Exception e) { - throw new ScriptException(e); + throw Context.throwAsScriptRuntimeEx(e); } } @@ -304,11 +302,8 @@ simpleScriptable.findMatchingScriptable(start, setterMethod), new Object[]{ newValue } ); } - catch( final InvocationTargetException e ) { - throw new ScriptException(e.getTargetException()); - } catch( final Exception e ) { - throw new ScriptException(e); + throw Context.throwAsScriptRuntimeEx(e); } } @@ -397,11 +392,8 @@ try { scriptable = (SimpleScriptable) javaScriptClass.newInstance(); } - catch (final InstantiationException e) { - throw new ScriptException(e); - } - catch (final IllegalAccessException e) { - throw new ScriptException(e); + catch (final Exception e) { + throw Context.throwAsScriptRuntimeEx(e); } } // parent scope needs to be set to the enclosing "window" (no simple unit test found to illustrate the Index: JavaScriptEngine.java =================================================================== RCS file: /cvsroot/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit/javascript/JavaScriptEngine.java,v retrieving revision 1.60 retrieving revision 1.61 diff -Lsrc/java/com/gargoylesoftware/htmlunit/javascript/JavaScriptEngine.java -Lsrc/java/com/gargoylesoftware/htmlunit/javascript/JavaScriptEngine.java -u -d -r1.60 -r1.61 --- src/java/com/gargoylesoftware/htmlunit/javascript/JavaScriptEngine.java +++ src/java/com/gargoylesoftware/htmlunit/javascript/JavaScriptEngine.java @@ -85,7 +85,7 @@ private static final ThreadLocal javaScriptRunning_ = new ThreadLocal(); private static final ContextListener contextListener_; - + /** * Key used to place the scope in which the execution of some javascript code * started (in {@link #callFunction} or {@link #execute}) as thread local attribute @@ -142,7 +142,7 @@ } catch( final Exception e ) { getLog().error("Exception while initializing JavaScript for the page", e); - throw new ScriptException(e); + throw new ScriptException(htmlPage, e); } finally { Context.exit(); @@ -250,12 +250,12 @@ return result; } catch (final Exception e ) { - final ScriptException scriptException = new ScriptException( e, sourceCode ); + final ScriptException scriptException = new ScriptException(htmlPage, e, sourceCode ); if (getWebClient().isThrowExceptionOnScriptError()) { throw scriptException; } else { - // use a ScriptException to log it because it provides good information + // use a ScriptException to log it because it provides good information // on the source code getLog().info("Catched script exception", scriptException); return null; @@ -291,7 +291,7 @@ else { scope = (Window) htmlPage.getEnclosingWindow().getScriptObject(); } - // some js code (like onchange handlers) should not be triggered from JS code: + // some js code (like onchange handlers) should not be triggered from JS code: // => keep trace of JS running or not final Boolean javaScriptAlreadyRunning = (Boolean) javaScriptRunning_.get(); javaScriptRunning_.set(Boolean.TRUE); @@ -304,12 +304,12 @@ } catch (final Exception e ) { final String sourceCode = context.decompileFunction(function, 2); - final ScriptException scriptException = new ScriptException( e, sourceCode); + final ScriptException scriptException = new ScriptException(htmlPage, e, sourceCode); if (getWebClient().isThrowExceptionOnScriptError()) { throw scriptException; } else { - // use a ScriptException to log it because it provides good information + // use a ScriptException to log it because it provides good information // on the source code getLog().info("Catched script exception", scriptException); return null; Index: ScriptException.java =================================================================== RCS file: /cvsroot/htmlunit/htmlunit/src/java/com/gargoylesoftware/htmlunit/ScriptException.java,v retrieving revision 1.15 retrieving revision 1.16 diff -Lsrc/java/com/gargoylesoftware/htmlunit/ScriptException.java -Lsrc/java/com/gargoylesoftware/htmlunit/ScriptException.java -u -d -r1.15 -r1.16 --- src/java/com/gargoylesoftware/htmlunit/ScriptException.java +++ src/java/com/gargoylesoftware/htmlunit/ScriptException.java @@ -43,33 +43,40 @@ import java.io.PrintWriter; import java.io.StringReader; import java.io.StringWriter; + import org.mozilla.javascript.EcmaError; import org.mozilla.javascript.JavaScriptException; import org.mozilla.javascript.WrappedException; +import com.gargoylesoftware.htmlunit.html.HtmlPage; + /** * An exception that will be thrown if an error occurs during the processing of * a script. * - * @version $Revision$ - * @author <a href="mailto:mbowler@xxxxxxxxxxxxxxxxxxxx">Mike Bowler</a> + * @version $Revision$ + * @author <a href="mailto:mbowler@xxxxxxxxxxxxxxxxxxxx">Mike Bowler</a> + * @author Marc Guillemot */ public class ScriptException extends RuntimeException { private static final long serialVersionUID = 4788896649084231283L; private final String scriptSourceCode_; + private final HtmlPage page_; /** * Create an instance - * + * @param page the page in which the script causing this exception was executed * @param throwable The exception that was thrown from the script engine. * @param scriptSourceCode The code that was being executed when this exception * was thrown. This may be null if the exception was not caused by execution * of javascript. */ - public ScriptException( final Throwable throwable, final String scriptSourceCode ) { + public ScriptException(final HtmlPage page, final Throwable throwable, + final String scriptSourceCode ) { super(getMessageFrom(throwable), throwable); scriptSourceCode_ = scriptSourceCode; + page_ = page; } private static String getMessageFrom( final Throwable throwable ) { @@ -83,11 +90,11 @@ /** * Create an instance - * + * @param page the page in which the script causing this exception was executed * @param throwable The exception that was thrown from the script engine. */ - public ScriptException( final Throwable throwable ) { - this( throwable, null ); + public ScriptException(final HtmlPage page, final Throwable throwable ) { + this(page, throwable, null); } @@ -259,4 +266,14 @@ return -1; } + + /** + * Gets the html page in which the script error occured.<br/> + * Caution: this page may be only partially parsed if the exception occured in a script + * executed at parsing time. + * @return the page + */ + public HtmlPage getPage() { + return page_; + } } ------------------------------------------------------- This SF.Net email is sponsored by xPML, a groundbreaking scripting language that extends applications into web and mobile media. Attend the live webcast and join the prime developer group breaking into this new coding territory! http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
|
|
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| Previous by Date: | [HtmlUnit] CVS Commit: ElementArray.java: removed workaround for Jaxen bug 55, mguillem |
|---|---|
| Next by Date: | Re: [HtmlUnit] CVS Commit: /htmlunit: fixed dependencies description page, D&J Gredler |
| Previous by Thread: | [HtmlUnit] CVS Commit: ElementArray.java: removed workaround for Jaxen bug 55, mguillem |
| Next by Thread: | [HtmlUnit] CVS Commit: build.xml: same change as maven, yourgod |
| Indexes: | [Date] [Thread] [Top] [All Lists] |
| News | FAQ | advertise |