How to get the JavaFx WebEngine to report errors in detail?
Asked Answered
S

3

12

In JavaFx I can attach a listener to the load worker for a webEngine like this:

 webEngine.getLoadWorker().stateProperty().addListener(
      new ChangeListener<Worker.State>() {
      public void changed(ObservableValue ov, Worker.State oldState, Worker.State newState) {                               
             System.out.println("webEngine result "+ newState.toString());
      }
  });

However if I try to load a document at an https address such as:

https://SomeLocalMachine.com:9443/jts/admin#action=com.ibm.team.repository.manageUsers

all I get printed out on the console is:

webEngine result READY
webEngine result SCHEDULED
webEngine result RUNNING
webEngine result FAILED

(The same https address in Firefox or Chrome gets me a login page)

Does anyone know how I can get more detailed reports out of the JavaFx WebEngine. I don't want to just know that it failed - I need to know why. I can guess my error is SSL/certificate/HTTPS related but currently I'm quite in the dark as to which part of SSL caused it to 'FAIL'

Southbound answered 20/3, 2013 at 11:30 Comment(0)
S
8

The best we ever got was:

if (webEngine.getLoadWorker().getException() != null && newState == State.FAILED) {
    exceptionMessage = ", " + webEngine.getLoadWorker().getException().toString();
}

but that didn't help.

(Our error was caused by a missing CookieStore, it seems you don't get one for free - and have to set a default one: http://docs.oracle.com/javase/7/docs/api/java/net/CookieHandler.html)

Southbound answered 11/10, 2013 at 8:15 Comment(2)
Where did you put that code? In the web engine load worker state property change listener?Regrate
Yes in ChangeListener as in ' webEngine.getLoadWorker().stateProperty().addListener( new ChangeListener<State>().....Southbound
M
19

You can use com.sun.javafx.webkit.WebConsoleListener. Downside is that it is JRE internal API.

WebConsoleListener.setDefaultListener(new WebConsoleListener(){
    @Override
    public void messageAdded(WebView webView, String message, int lineNumber, String sourceId) {
        System.out.println("Console: [" + sourceId + ":" + lineNumber + "] " + message);
    }
});
Myatt answered 10/6, 2016 at 17:1 Comment(4)
This method does not exist >= Java 11Untangle
why the F would they take this out without adding a corresponding public API to handle the console?Olid
re "JRE internal API" / "does not exist >= Java 11". If you are using JavaFX as a module, this is indeed not accessible (not exported by the javafx module). However, if you're using the unsupported-but-working javaFX-as-jar-dependency setup, you can use WebConsoleListener.setDefaultListener() Source: I just used this method yesterday, on JavaFX 17.0.2 on JDK 17.Ikeikebana
See also: What is the public API for getting JavaFX WebView console events?Ikeikebana
N
8

Have you tried the following:

engine.getLoadWorker().exceptionProperty().addListener(new ChangeListener<Throwable>() {
    @Override
    public void changed(ObservableValue<? extends Throwable> ov, Throwable t, Throwable t1) {
        System.out.println("Received exception: "+t1.getMessage());
    }
});
Niggle answered 10/10, 2013 at 14:4 Comment(0)
S
8

The best we ever got was:

if (webEngine.getLoadWorker().getException() != null && newState == State.FAILED) {
    exceptionMessage = ", " + webEngine.getLoadWorker().getException().toString();
}

but that didn't help.

(Our error was caused by a missing CookieStore, it seems you don't get one for free - and have to set a default one: http://docs.oracle.com/javase/7/docs/api/java/net/CookieHandler.html)

Southbound answered 11/10, 2013 at 8:15 Comment(2)
Where did you put that code? In the web engine load worker state property change listener?Regrate
Yes in ChangeListener as in ' webEngine.getLoadWorker().stateProperty().addListener( new ChangeListener<State>().....Southbound

© 2022 - 2024 — McMap. All rights reserved.