use of com.company.project.core.ApplicationException in project spring-boot-api-seed-project by selfassu.
the class WebMvcConfigurer method configureHandlerExceptionResolvers.
@Override
protected void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
exceptionResolvers.add(new HandlerExceptionResolver() {
@Nullable
@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception exception) {
Result result = new Result();
if (exception instanceof ApplicationException) {
// 业务失败的异常,手动抛出的异常
result.setCode(ResultCode.FAILED).setMessage(exception.getMessage());
logger.info(exception.getMessage());
} else if (exception instanceof NoHandlerFoundException) {
// 请求路径没有找到
result.setCode(ResultCode.NOT_FOUND).setMessage("接口 {" + request.getRequestURI() + "} 不存在,请检查");
logger.info(exception.getMessage());
} else if (exception instanceof ServletException) {
result.setCode(ResultCode.FAILED).setMessage(exception.getMessage());
logger.info(exception.getMessage());
} else {
result.setCode(ResultCode.SERVER_ERROR).setMessage("服务器出错!接口 {" + request.getRequestURI() + "} 无法执行,请联系管理员!");
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(), exception.getMessage());
} else {
message = exception.getMessage();
}
logger.error(message, exception);
}
responseResult(response, result);
return new ModelAndView();
}
});
}
Aggregations