GWT Platform with REST dispatch: get http status code from exception handler

时间:2016-12-02 05:19:44

标签: gwt gwtp gwt-platform

I'm usng GWTP rest dispath with ResourceDelegate and I want to catch all exceptions in client REST requests. My REST backend return:

  • 401 or 403 status codes if no authorization/forbidden
  • 500 for other errors

So, I have added common handler to 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;
    }
}

I found that all REST exception are instances of ActionException class. How can I get HTTP status code and http response body inside MyRestExceptionHandler?

1 个答案:

答案 0 :(得分: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())