码迷,mamicode.com
首页 > 其他好文 > 详细

自定义全局异常处理器

时间:2019-05-24 19:29:52      阅读:98      评论:0      收藏:0      [点我收藏+]

标签:his   redirect   ret   use   close   url   ==   The   case   

public class RtopExceptionHandler implements HandlerExceptionResolver{

private static final Logger logger = LoggerFactory.getLogger(RtopExceptionHandler.class);


@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object o,
Exception ex) {

//处理buservice的异常
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");

if(ex instanceof AccessException) {
if(BuserviceConstants.HANDLER_DIGEST_LOGGER.isInfoEnabled()) {
BuserviceConstants.HANDLER_DIGEST_LOGGER.info("The current request failed, the reason:" + ex);
}

AccessException e = (AccessException)ex;
String redirectUrl = e.getRedirectUrl();
String requestURI = request.getRequestURI();
if(StringUtils.endsWithIgnoreCase(requestURI, ".json")) {
try {
ErrorCodeEnum errorCode = e.getErrorCode();
String buserviceErrorCode = errorCode == null? ErrorCodeEnum.UNKNOW.name():errorCode.name();
String buserviceErrorMsg = errorCode == null? ErrorCodeEnum.UNKNOW.getDescCn():errorCode.getDescCn();

RtopHttpResult result = RtopHttpResult.Failure(buserviceErrorCode, buserviceErrorMsg);

if(ErrorCodeEnum.USER_NOT_LOGIN == errorCode){
//如果返回的错误是用户未登录,则将请求强制改为success,为了防止前端弹框展示错误信息.由前端直接跳转到登录页面.
result.setSuccess(true);
}

PrintWriter pw = response.getWriter();
String callback = request.getParameter("callback");
if(StringUtil.isNotBlank(callback)) {
pw.print(callback + "(" + JSON.toJSONString(result) + ")");
} else {
pw.print(JSON.toJSON(result));
}

pw.close();
} catch (Exception var13) {
BuserviceConstants.HANDLER_DIGEST_LOGGER.info("The current JSON request fails, intercepting failure:" + var13);
}

} else {
return StringUtil.isNotBlank(redirectUrl)?this.returnPage("redirect:" + redirectUrl, ex, new ModelAndView()):this.returnPage("/error", ex, new ModelAndView());

}
}else if(ex instanceof ErrorCodeException){
ErrorCodeException errorCodeException = (ErrorCodeException) ex;
LoggerUtil.error(errorCodeException, "");

String errorCode = errorCodeException.getErrorCode().getCode();
String errorMsg = errorCodeException.getErrorCode().getMessage();
RtopHttpResult result = RtopHttpResult.Failure(errorCode, errorMsg);

try{
PrintWriter pw = response.getWriter();
pw.print(JSON.toJSON(result));
pw.close();
}catch (Exception e) {
LoggerUtil.info(logger, "response in RtopExceptionHandler error" + e.getMessage());
}

}else if(ex instanceof RuntimeException){
if(ex instanceof IllegalStateException){
return null;
}
LoggerUtil.error(ex, "");

RtopHttpResult result = RtopHttpResult.Failure(RtopAppErrorCode.INTERNAL_ERROR);

try{
PrintWriter pw = response.getWriter();
pw.print(JSON.toJSON(result));
pw.close();
}catch (Exception e) {
LoggerUtil.info(logger, "response in RtopExceptionHandler error" + e.getMessage());
}

}

return null;
}

public ModelAndView returnPage(String page, Exception ex, ModelAndView view) {
view.addObject("exception", ex);
view.setViewName(page);
return view;
}
}

自定义全局异常处理器

标签:his   redirect   ret   use   close   url   ==   The   case   

原文地址:https://www.cnblogs.com/wwqwwq/p/10919674.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!