Resty-GWT和HandlerExceptionResolver:我如何处理特殊情况?

时间:2013-10-08 15:54:03

标签: gwt spring-mvc exception-handling resty

我正在为我的网络应用使用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()方法识别这两个特殊情况?

0 个答案:

没有答案