0

我在我的客户端上使用 GWTP 和 RequestFactory。

我希望任何致命异常都由自定义UncaughtExceptionHandler处理。我已经创建了我的自定义处理程序并在我的入口点模块的 configure() 调用中注册了它:

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

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

但是,如果在我的客户端上我抛出一个异常

throw new RuntimeException("test");

未捕获异常。在开发模式下,我看到未捕获的异常一直到开发控制台。进一步调试显示 GWT 没有注册我的自定义处理程序:

handler = GWT.getUncaughtExceptionHandler();

返回

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

关于为什么 GWT.setUncaughtExceptionHandler 不起作用的任何想法?

作为记录,我通过 cleancodematters关注了这篇文章。他的实现和我的唯一区别是我在客户端使用 GWTP(和 GIN)。

4

3 回答 3

1

onFailure方法肯定会被调用,因为您可以覆盖它并获得有效响应,因此请查看以下的默认实现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());
  }
}

在您的测试用例中,收到的错误是致命错误吗?如果错误没有被标记为致命错误,那么默认实现将完全按照您所看到的那样做......什么也没有。

于 2013-10-15T19:03:09.847 回答
1

我认为你不能使用 GINClientModule来设置你的 UncaughtExceptionHandler. 而是创建一个自定义 PreBootstrapper

PreBootstrapper 允许您在 GWTP 引导过程之前挂钩
开始。如果您需要在 GWTP 启动之前完成某些操作,这将特别有用。在
一般建议使用引导程序,但在某些情况下不是
足够了,例如在为 gwt-log 设置 UncaughtExceptionHandler 时。
<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");
            }
        });
    }
} 
于 2013-10-16T14:21:48.880 回答
0

在回调中运行的任何 JS 代码都必须使用 $entry 将调用包装到 GWT 中,以便正确路由任何未捕获的异常。如果这没有发生听起来像是 GWTP 中的错误(或者可能是 RequestFactory,尽管这似乎不太可能,因为它是 GWT 的一部分)。

于 2013-10-16T00:37:32.297 回答