|
|
|
@ -24,6 +24,7 @@ import org.springframework.web.servlet.config.annotation.InterceptorRegistry; |
|
|
|
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; |
|
|
|
|
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; |
|
|
|
|
|
|
|
|
|
import javax.servlet.ServletException; |
|
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
|
import java.io.IOException; |
|
|
|
@ -62,28 +63,27 @@ public class WebMvcConfigurer extends WebMvcConfigurerAdapter { |
|
|
|
|
exceptionResolvers.add(new HandlerExceptionResolver() { |
|
|
|
|
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception e) { |
|
|
|
|
Result result = new Result(); |
|
|
|
|
if (handler instanceof HandlerMethod) { |
|
|
|
|
HandlerMethod handlerMethod = (HandlerMethod) handler; |
|
|
|
|
|
|
|
|
|
if (e instanceof ServiceException) {//业务失败的异常,如“账号或密码错误”
|
|
|
|
|
result.setCode(ResultCode.FAIL).setMessage(e.getMessage()); |
|
|
|
|
logger.info(e.getMessage()); |
|
|
|
|
} else if (e instanceof NoHandlerFoundException) { |
|
|
|
|
result.setCode(ResultCode.NOT_FOUND).setMessage("接口 [" + request.getRequestURI() + "] 不存在"); |
|
|
|
|
} else if (e instanceof ServletException) { |
|
|
|
|
result.setCode(ResultCode.FAIL).setMessage(e.getMessage()); |
|
|
|
|
} else { |
|
|
|
|
result.setCode(ResultCode.INTERNAL_SERVER_ERROR).setMessage("接口 [" + request.getRequestURI() + "] 内部错误,请联系管理员"); |
|
|
|
|
String message = String.format("接口 [%s] 出现异常,方法:%s.%s,异常摘要:%s", |
|
|
|
|
String message; |
|
|
|
|
if (handler instanceof HandlerMethod) { |
|
|
|
|
HandlerMethod handlerMethod = (HandlerMethod) handler; |
|
|
|
|
message = String.format("接口 [%s] 出现异常,方法:%s.%s,异常摘要:%s", |
|
|
|
|
request.getRequestURI(), |
|
|
|
|
handlerMethod.getBean().getClass().getName(), |
|
|
|
|
handlerMethod.getMethod().getName(), |
|
|
|
|
e.getMessage()); |
|
|
|
|
logger.error(message, e); |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
if (e instanceof NoHandlerFoundException) { |
|
|
|
|
result.setCode(ResultCode.NOT_FOUND).setMessage("接口 [" + request.getRequestURI() + "] 不存在"); |
|
|
|
|
} else { |
|
|
|
|
result.setCode(ResultCode.INTERNAL_SERVER_ERROR).setMessage(e.getMessage()); |
|
|
|
|
logger.error(e.getMessage(), e); |
|
|
|
|
message = e.getMessage(); |
|
|
|
|
} |
|
|
|
|
result.setCode(ResultCode.INTERNAL_SERVER_ERROR).setMessage(message); |
|
|
|
|
logger.error(e.getMessage(), e); |
|
|
|
|
} |
|
|
|
|
responseResult(response, result); |
|
|
|
|
return new ModelAndView(); |
|
|
|
|