我正在为我的网络应用程序使用 GWT 和 Resty。
我需要将两个异常视为特殊情况并将附加信息传递给客户。这些以前由 Resty 调用 onFailure() 方法处理,如下所示:
getClientFactory().getRequest().restyMethod(id,
new MethodRecipient<DataDTO>() {
@Override
public void onSuccess(Method method, DataDTO response) {
// do stuff
}
@Override
public void onFailure(Method method, Throwable ex) {
if (ex.getMessage().endsWith("exception message 1")) {
getClientFactory().getAppController().showRestFatalError("Message 1", "Title 1", errorMsg);
} else if (ex.getMessage().endsWith("exception message 2")) {
getClientFactory().getAppController().showRestFatalError("Message 2", "Title 2", errorMsg);
} else {
// else fall back to a friendly but vague "something else went wrong"
getClientFactory().getAppController().showRestFatalError("Unexpected error", "Unexpected Error", errorMsg);
}
}
});
为了在所有异常到达客户端之前捕获并记录它们,我实现了一个自定义 Spring MVC HandlerExceptionResolver,如下所示:
我有一个自定义的 HandlerExceptionResolver 用于记录控制器错误,如下所示:
@Component
public class LoggingHandlerExceptionResolver extends AbstractHandlerExceptionResolver {
private final Logger logger = LoggerFactory.getLogger(LoggingHandlerExceptionResolver.class);
@Override
public ModelAndView doResolveException(HttpServletRequest aReq, HttpServletResponse aRes, Object aHandler, Exception ex) {
logger.error("Server exception thrown", ex);
return null; // trigger other HandlerExceptionResolvers
}
}
使用 HandlerExceptionResolver 时,只有状态码 500 被传递给客户端。如何让 onFailure() 方法识别这两种特殊情况?