Search in sources :

Example 1 with ApplicationException

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();
        }
    });
}
Also used : HandlerExceptionResolver(org.springframework.web.servlet.HandlerExceptionResolver) ModelAndView(org.springframework.web.servlet.ModelAndView) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletException(javax.servlet.ServletException) NoHandlerFoundException(org.springframework.web.servlet.NoHandlerFoundException) ApplicationException(com.company.project.core.ApplicationException) HandlerMethod(org.springframework.web.method.HandlerMethod) Result(com.company.project.core.Result) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ApplicationException(com.company.project.core.ApplicationException) NoHandlerFoundException(org.springframework.web.servlet.NoHandlerFoundException) Nullable(org.springframework.lang.Nullable)

Aggregations

ApplicationException (com.company.project.core.ApplicationException)1 Result (com.company.project.core.Result)1 ServletException (javax.servlet.ServletException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Nullable (org.springframework.lang.Nullable)1 HandlerMethod (org.springframework.web.method.HandlerMethod)1 HandlerExceptionResolver (org.springframework.web.servlet.HandlerExceptionResolver)1 ModelAndView (org.springframework.web.servlet.ModelAndView)1 NoHandlerFoundException (org.springframework.web.servlet.NoHandlerFoundException)1