Search in sources :

Example 1 with JavaScriptException

use of net.sourceforge.htmlunit.corejs.javascript.JavaScriptException in project htmlunit by HtmlUnit.

the class HTMLCollectionFrames method triggerOnError.

/**
 * Triggers the {@code onerror} handler, if one has been set.
 * @param e the error that needs to be reported
 */
public void triggerOnError(final ScriptException e) {
    final Object o = getOnerror();
    if (o instanceof Function) {
        final Function f = (Function) o;
        String msg = e.getMessage();
        final String url = e.getPage().getUrl().toExternalForm();
        final int line = e.getFailingLineNumber();
        final int column = e.getFailingColumnNumber();
        Object jsError = e.getMessage();
        if (e.getCause() instanceof JavaScriptException) {
            msg = "uncaught exception: " + e.getCause().getMessage();
            jsError = ((JavaScriptException) e.getCause()).getValue();
        } else if (e.getCause() instanceof EcmaError) {
            msg = "uncaught " + e.getCause().getMessage();
            final EcmaError ecmaError = (EcmaError) e.getCause();
            final Scriptable err = Context.getCurrentContext().newObject(this, "Error");
            ScriptableObject.putProperty(err, "message", ecmaError.getMessage());
            ScriptableObject.putProperty(err, "fileName", ecmaError.sourceName());
            ScriptableObject.putProperty(err, "lineNumber", Integer.valueOf(ecmaError.lineNumber()));
            jsError = err;
        }
        final Object[] args = { msg, url, Integer.valueOf(line), Integer.valueOf(column), jsError };
        f.call(Context.getCurrentContext(), this, this, args);
    }
}
Also used : JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction) Function(net.sourceforge.htmlunit.corejs.javascript.Function) EcmaError(net.sourceforge.htmlunit.corejs.javascript.EcmaError) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) HtmlObject(com.gargoylesoftware.htmlunit.html.HtmlObject) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable) HtmlUnitScriptable(com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable) JavaScriptException(net.sourceforge.htmlunit.corejs.javascript.JavaScriptException)

Example 2 with JavaScriptException

use of net.sourceforge.htmlunit.corejs.javascript.JavaScriptException in project htmlunit by HtmlUnit.

the class ScriptException method createPrintableStackTrace.

private String createPrintableStackTrace() {
    final StringWriter stringWriter = new StringWriter();
    final PrintWriter printWriter = new PrintWriter(stringWriter);
    printWriter.println("======= EXCEPTION START ========");
    if (getCause() != null) {
        if (getCause() instanceof EcmaError) {
            final EcmaError ecmaError = (EcmaError) getCause();
            printWriter.print("EcmaError: ");
            printWriter.print("lineNumber=[");
            printWriter.print(ecmaError.lineNumber());
            printWriter.print("] column=[");
            printWriter.print(ecmaError.columnNumber());
            printWriter.print("] lineSource=[");
            printWriter.print(getFailingLine());
            printWriter.print("] name=[");
            printWriter.print(ecmaError.getName());
            printWriter.print("] sourceName=[");
            printWriter.print(ecmaError.sourceName());
            printWriter.print("] message=[");
            printWriter.print(ecmaError.getMessage());
            printWriter.print("]");
            printWriter.println();
        } else {
            printWriter.println("Exception class=[" + getCause().getClass().getName() + "]");
        }
    }
    super.printStackTrace(printWriter);
    if (getCause() instanceof JavaScriptException) {
        final Object value = ((JavaScriptException) getCause()).getValue();
        printWriter.print("JavaScriptException value = ");
        if (value instanceof Throwable) {
            ((Throwable) value).printStackTrace(printWriter);
        } else {
            printWriter.println(value);
        }
    } else if (getCause() instanceof WrappedException) {
        final WrappedException wrappedException = (WrappedException) getCause();
        printWriter.print("WrappedException: ");
        wrappedException.printStackTrace(printWriter);
        final Throwable innerException = wrappedException.getWrappedException();
        if (innerException == null) {
            printWriter.println("Inside wrapped exception: null");
        } else {
            printWriter.println("Inside wrapped exception:");
            innerException.printStackTrace(printWriter);
        }
    } else if (getCause() != null) {
        printWriter.println("Enclosed exception: ");
        getCause().printStackTrace(printWriter);
    }
    if (scriptSourceCode_ != null && !scriptSourceCode_.isEmpty()) {
        printWriter.println("== CALLING JAVASCRIPT ==");
        printWriter.println(scriptSourceCode_);
    }
    printWriter.println("======= EXCEPTION END ========");
    return stringWriter.toString();
}
Also used : WrappedException(net.sourceforge.htmlunit.corejs.javascript.WrappedException) EcmaError(net.sourceforge.htmlunit.corejs.javascript.EcmaError) StringWriter(java.io.StringWriter) PrintWriter(java.io.PrintWriter) JavaScriptException(net.sourceforge.htmlunit.corejs.javascript.JavaScriptException)

Example 3 with JavaScriptException

use of net.sourceforge.htmlunit.corejs.javascript.JavaScriptException in project htmlunit by HtmlUnit.

the class HtmlUnitRegExpProxy2Test method needCustomFix.

/**
 * Tests if custom patch is still needed.
 */
@Test
public void needCustomFix() {
    final WebClient client = getWebClient();
    final ContextFactory cf = ((JavaScriptEngine) client.getJavaScriptEngine()).getContextFactory();
    final Context ctx = cf.enterContext();
    try {
        final ScriptableObject topScope = ctx.initStandardObjects();
        topScope.put("str", topScope, str_);
        topScope.put("text", topScope, text_);
        topScope.put("expected", topScope, expected_);
        assertEquals(begin_ + end_, text_.replaceAll(str_, ""));
        try {
            ctx.evaluateString(topScope, src_, "test script", 0, null);
        } catch (final JavaScriptException e) {
            assertTrue(e.getMessage().indexOf("Expected >") == 0);
        }
    } finally {
        Context.exit();
    }
}
Also used : ContextFactory(net.sourceforge.htmlunit.corejs.javascript.ContextFactory) Context(net.sourceforge.htmlunit.corejs.javascript.Context) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) WebClient(com.gargoylesoftware.htmlunit.WebClient) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine) JavaScriptException(net.sourceforge.htmlunit.corejs.javascript.JavaScriptException) Test(org.junit.Test)

Example 4 with JavaScriptException

use of net.sourceforge.htmlunit.corejs.javascript.JavaScriptException in project htmlunit by HtmlUnit.

the class HtmlUnitRegExpProxy2Test method matchFixNeeded.

/**
 * Test if the custom fix is needed or not. When this test fails, then it means that the problem is solved in
 * Rhino and that custom fix for String.match in {@link HtmlUnitRegExpProxy} is not needed anymore (and that
 * this test can be removed, or turned positive).
 * @throws Exception if the test fails
 */
@Test
public void matchFixNeeded() throws Exception {
    final WebClient client = getWebClient();
    final ContextFactory cf = ((JavaScriptEngine) client.getJavaScriptEngine()).getContextFactory();
    final Context cx = cf.enterContext();
    try {
        final ScriptableObject topScope = cx.initStandardObjects();
        cx.evaluateString(topScope, scriptTestMatch_, "test script String.match", 0, null);
        try {
            cx.evaluateString(topScope, scriptTestMatch_, "test script String.match", 0, null);
        } catch (final JavaScriptException e) {
            assertTrue(e.getMessage().indexOf("Expected >") == 0);
        }
    } finally {
        Context.exit();
    }
}
Also used : ContextFactory(net.sourceforge.htmlunit.corejs.javascript.ContextFactory) Context(net.sourceforge.htmlunit.corejs.javascript.Context) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) WebClient(com.gargoylesoftware.htmlunit.WebClient) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine) JavaScriptException(net.sourceforge.htmlunit.corejs.javascript.JavaScriptException) Test(org.junit.Test)

Example 5 with JavaScriptException

use of net.sourceforge.htmlunit.corejs.javascript.JavaScriptException in project htmlunit by HtmlUnit.

the class Node method asJavaScriptException.

/**
 * Encapsulates the given {@link DOMException} into a Rhino-compatible exception.
 *
 * @param exception the exception to encapsulate
 * @return the created exception
 */
protected RhinoException asJavaScriptException(final DOMException exception) {
    final Window w = getWindow();
    exception.setPrototype(w.getPrototype(exception.getClass()));
    exception.setParentScope(w);
    // get current line and file name
    // this method can only be used in interpreted mode. If one day we choose to use compiled mode,
    // then we'll have to find an other way here.
    final String fileName;
    final int lineNumber;
    if (Context.getCurrentContext().getOptimizationLevel() == -1) {
        final int[] linep = new int[1];
        final String sourceName = new Interpreter().getSourcePositionFromStack(Context.getCurrentContext(), linep);
        fileName = sourceName.replaceFirst("script in (.*) from .*", "$1");
        lineNumber = linep[0];
    } else {
        throw new Error("HtmlUnit not ready to run in compiled mode");
    }
    exception.setLocation(fileName, lineNumber);
    return new JavaScriptException(exception, fileName, lineNumber);
}
Also used : Window(com.gargoylesoftware.htmlunit.javascript.host.Window) Interpreter(net.sourceforge.htmlunit.corejs.javascript.Interpreter) JavaScriptException(net.sourceforge.htmlunit.corejs.javascript.JavaScriptException)

Aggregations

JavaScriptException (net.sourceforge.htmlunit.corejs.javascript.JavaScriptException)7 Context (net.sourceforge.htmlunit.corejs.javascript.Context)3 ScriptableObject (net.sourceforge.htmlunit.corejs.javascript.ScriptableObject)3 WebClient (com.gargoylesoftware.htmlunit.WebClient)2 JavaScriptEngine (com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine)2 ContextFactory (net.sourceforge.htmlunit.corejs.javascript.ContextFactory)2 EcmaError (net.sourceforge.htmlunit.corejs.javascript.EcmaError)2 Scriptable (net.sourceforge.htmlunit.corejs.javascript.Scriptable)2 Test (org.junit.Test)2 HtmlObject (com.gargoylesoftware.htmlunit.html.HtmlObject)1 HtmlUnitScriptable (com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable)1 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)1 Window (com.gargoylesoftware.htmlunit.javascript.host.Window)1 BaseTest (com.github.sergueik.selenium.BaseTest)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 ContextAction (net.sourceforge.htmlunit.corejs.javascript.ContextAction)1 Function (net.sourceforge.htmlunit.corejs.javascript.Function)1 Interpreter (net.sourceforge.htmlunit.corejs.javascript.Interpreter)1 WrappedException (net.sourceforge.htmlunit.corejs.javascript.WrappedException)1