Question

I am using GWTP and RequestFactory on my client.

I would like any Fatal Exception to be handled by a custom UncaughtExceptionHandler. I have created my custom handler and registered it in the configure() call of my entry point module:

public class ClientModule extends AbstractPresenterModule {
    @Override
    protected void configure() {

        // Register Uncaught Exception Handler first thing
        GWT.setUncaughtExceptionHandler( new CustomUncaughtExceptionHandler() );
...

However, if on my client I throw an Exception

throw new RuntimeException("test");

The exception is not captured. In dev mode, I see the uncaught exception go all the way to the development console. Further debugging shows that GWT has not registered my custom handler:

handler = GWT.getUncaughtExceptionHandler();

returns

com.google.gwt.core.client.GWT$DefaultUncaughtExceptionHandler@370563b1

Any ideas on why GWT.setUncaughtExceptionHandler is not working?

For the record, I followed this post by cleancodematters. The only difference between his implementation and mine is that I use GWTP (and GIN) on the client.

Was it helpful?

Solution

I think you can't use GIN's ClientModule to set your UncaughtExceptionHandler. Instead create a custom PreBootstrapper:

A PreBootstrapper allows you to hook into the GWTP bootstrapping process right before it
starts. This is particularly useful if you need something done before GWTP starts up. In 
general the use of a Bootstrapper is advised but there are cases where that is not
enough,for example when setting up an UncaughtExceptionHandler for gwt-log.
<set-configuration-property name="gwtp.prebootstrapper"  
         value="com.arcbees.project.client.PreBootstrapperImpl"/> 

public class PreBootstrapperImpl implements PreBootstrapper {
    @Override
    public void onPreBootstrap() {
        GWT.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
            @Override
            public void onUncaughtException(final Throwable e) {
                Window.alert("There was a problem loading your application");
            }
        });
    }
} 

OTHER TIPS

The onFailure method is definitely being called since you can override it and get a valid response, so looking at the default implementation for Receiver#onFailure:

/**
 * Receives general failure notifications. The default implementation looks at
 * {@link ServerFailure#isFatal()}, and throws a runtime exception with the
 * failure object's error message if it is true.
 * 
 * @param error a {@link ServerFailure} instance
 */
public void onFailure(ServerFailure error) {
  if (error.isFatal()) {
    throw new RuntimeException(error.getMessage());
  }
}

In your test case, is the error being received as a fatal error? If the error isn't marked as fatal, then the default implementation will do exactly what you're seeing ... nothing.

Any JS code run in a callback must wrap calls into GWT with $entry so that any uncaught exceptions get routed properly. If that isn't happening sounds like a bug in GWTP (or possibly RequestFactory, though that seems less likely since it is part of GWT).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top