Search in sources :

Example 1 with EcmaError

use of net.sourceforge.htmlunit.corejs.javascript.EcmaError 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 EcmaError

use of net.sourceforge.htmlunit.corejs.javascript.EcmaError 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 EcmaError

use of net.sourceforge.htmlunit.corejs.javascript.EcmaError in project htmlunit-core-js by HtmlUnit.

the class ExceptionMessageTest method onlyGetterError.

private static void onlyGetterError(final int feature) {
    final ContextFactory cf = new ContextFactory() {

        @Override
        protected boolean hasFeature(Context cx, int featureIndex) {
            if (Context.FEATURE_STRICT_MODE == featureIndex) {
                return true;
            }
            return super.hasFeature(cx, featureIndex);
        }
    };
    final String script = "o.readonlyProp = 123";
    final ContextAction<Object> action = new ContextAction<Object>() {

        @Override
        public Object run(final Context cx) {
            try {
                Scriptable scope = cx.initSafeStandardObjects();
                final MyHostObject prototype = new MyHostObject();
                ScriptableObject.defineClass(scope, MyHostObject.class);
                final Method readMethod = MyHostObject.class.getMethod("jsxGet_x");
                prototype.defineProperty("readonlyProp", null, readMethod, null, ScriptableObject.EMPTY);
                ScriptableObject.defineProperty(scope, "o", prototype, ScriptableObject.DONTENUM);
                cx.evaluateString(scope, script, "test_script", 1, null);
                throw new RuntimeException("Should have failed!");
            } catch (final EcmaError e) {
                assertEquals("TypeError: Cannot set property [MyHostObject].readonlyProp that has only a getter to value '123'. (test_script#1)", e.getMessage());
                return null;
            } catch (final Exception e) {
                throw new RuntimeException(e);
            }
        }
    };
    Utils.runWithAllOptimizationLevels(cf, action);
}
Also used : ContextFactory(net.sourceforge.htmlunit.corejs.javascript.ContextFactory) Context(net.sourceforge.htmlunit.corejs.javascript.Context) EcmaError(net.sourceforge.htmlunit.corejs.javascript.EcmaError) ContextAction(net.sourceforge.htmlunit.corejs.javascript.ContextAction) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) Method(java.lang.reflect.Method) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable)

Example 4 with EcmaError

use of net.sourceforge.htmlunit.corejs.javascript.EcmaError in project htmlunit-core-js by HtmlUnit.

the class ExceptionMessageTest method exceptionMessage.

private static void exceptionMessage(final String script, final String expectedMesage) {
    final ContextAction<Object> action = new ContextAction<Object>() {

        @Override
        public Object run(final Context cx) {
            try {
                Scriptable scope = cx.initSafeStandardObjects();
                cx.evaluateString(scope, script, "test_script", 1, null);
                throw new RuntimeException("Should have failed!");
            } catch (final EcmaError e) {
                assertEquals(expectedMesage + " (test_script#1)", e.getMessage());
                return null;
            } catch (final Exception e) {
                throw new RuntimeException(e);
            }
        }
    };
    Utils.runWithAllOptimizationLevels(action);
}
Also used : Context(net.sourceforge.htmlunit.corejs.javascript.Context) EcmaError(net.sourceforge.htmlunit.corejs.javascript.EcmaError) ContextAction(net.sourceforge.htmlunit.corejs.javascript.ContextAction) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable)

Aggregations

EcmaError (net.sourceforge.htmlunit.corejs.javascript.EcmaError)4 Scriptable (net.sourceforge.htmlunit.corejs.javascript.Scriptable)3 ScriptableObject (net.sourceforge.htmlunit.corejs.javascript.ScriptableObject)3 Context (net.sourceforge.htmlunit.corejs.javascript.Context)2 ContextAction (net.sourceforge.htmlunit.corejs.javascript.ContextAction)2 JavaScriptException (net.sourceforge.htmlunit.corejs.javascript.JavaScriptException)2 HtmlObject (com.gargoylesoftware.htmlunit.html.HtmlObject)1 HtmlUnitScriptable (com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable)1 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 Method (java.lang.reflect.Method)1 ContextFactory (net.sourceforge.htmlunit.corejs.javascript.ContextFactory)1 Function (net.sourceforge.htmlunit.corejs.javascript.Function)1 WrappedException (net.sourceforge.htmlunit.corejs.javascript.WrappedException)1