我正在使用ResourceSelegate进行GWTP REST分配,我想在客户端ret REST请求中捕获所有例外。我休息的后端返回:
- 401或403状态代码,如果没有授权/禁止
- 500其他错误
所以,我在retdispatchasyncmodule中添加了普通处理程序:
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;
}
}
我发现所有休息异常都是ActionException类的实例。如何在MyrestexceptionHandler内获取HTTP状态代码和HTTP响应主体?
解决方案是使用restdispatchhooks而不是异常。
安装模块: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())