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