2013-10-15 23 views
0

我在客戶端上使用GWTP和RequestFactory。GWT的UncaughtExceptionHandler沒有被調用(使用GWTP和RF)

我想任何致命異常由自定義UncaughtExceptionHandler處理。我創建我的自定義處理程序,並在我的入口點模塊的配置()調用註冊它:

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

回報

[email protected]70563b1

任何想法上爲什麼GWT.setUncaughtExceptionHandler不工作?

對於記錄,我遵循cleancodematters這post。他的實現和我的唯一區別在於我在客戶端使用GWTP(和GIN)。

回答

1

我想你不能用GIN的ClientModule來設置你的UncaughtExceptionHandler。 而是創建一個自定義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"); 
      } 
     }); 
    } 
} 
+0

我肯定錯過了一點信息。感謝您指出! – manubot

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

在您的測試情況下,正在接收的錯誤致命錯誤?如果錯誤未被標記爲致命錯誤,那麼默認實現將完成您所看到的......沒有任何事情。

+0

感謝,你的觀點是正確的。但是,這個例外確實是致命的。我想我可以從這裏排除射頻工廠問題。看到我的更新Q – manubot

0

在回調中運行的任何JS代碼都必須用$ entry將調用包裝到GWT中,以便任何未捕獲的異常都能正確路由。如果這沒有發生,就像GWTP中的錯誤(或者可能是RequestFactory,儘管這似乎不太可能,因爲它是GWT的一部分)。

+0

我的應用程序上沒有自定義的JS代碼。這可能是一個GWTP的事情,請參閱我的Q更新,並讓我知道,如果你有任何想法 – manubot

相關問題