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);
}
}
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();
}
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);
}
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);
}
Aggregations