2016-12-02 26 views
0

我使用ResourceDelegate處理GWTP靜態調度,並且希望捕獲客戶端REST請求中的所有異常。 我的REST後端回報:帶REST調度的GWT平臺:從異常處理程序中獲取http狀態代碼

  • 401或403狀態碼,如果沒有授權/禁止
  • 500其他 錯誤

所以,我加入通用處理器來RestDispatchAsyncModule:

new RestDispatchAsyncModule.Builder().exceptionHandler(MyRestExceptionHandler.class); 

MyRestExceptionHandler.java:

public class MyRestExceptionHandler implements ExceptionHandler { 
    @Override 
    public Status onFailure(Throwable e) { 
     if (e instanceof ActionException){ 
      ActionException a = (ActionException)e; 
      // How to get HTTP status code and response body here? 
     } 
     return null; 
    } 
} 

我發現所有的REST異常都是ActionException類的實例。 如何在MyRestExceptionHandler中獲取HTTP狀態碼和http響應正文?

回答

0

解決方案是使用RestDispatchHooks而不是ExceptionHandler。

AppRestDispatchHooks.java:

public class AppRestDispatchHooks implements RestDispatchHooks { 
    @Override 
    public void onExecute(RestAction<?> action) { 
    } 

    @Override 
    public void onSuccess(RestAction<?> action, Response response, Object result) { 
    } 

    @Override 
    public void onFailure(RestAction<?> action, Response response, Throwable caught) { 
     GWT.log("Status code:"+ response.getStatusCode()); 
    } 
} 

安裝模塊:

install(new RestDispatchAsyncModule.Builder() 
     .dispatchHooks(AppRestDispatchHooks.class) 
     .build())