Search in sources :

Example 1 with ServiceException

use of com.rogge.common.core.ServiceException in project spring-cloud by Rogge666.

the class WebMvcConfigurer method configureHandlerExceptionResolvers.

// 统一异常处理
@Override
public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
    exceptionResolvers.add(new HandlerExceptionResolver() {

        public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception e) {
            ApiResponse lApiResponse = new ApiResponse();
            if (e instanceof ServiceException) {
                // 业务失败的异常,如“账号或密码错误”
                lApiResponse.setCode(ResponseCode.Base.ERROR);
                lApiResponse.setMsg(e.getMessage());
                logger.info(e.getMessage());
            } else if (e instanceof NoHandlerFoundException) {
                lApiResponse.setCode(ResponseCode.Base.API_NO_EXISTS);
                lApiResponse.setMsg("接口 [" + request.getRequestURI() + "] 不存在");
            } else if (e instanceof ServletException) {
                lApiResponse.setCode(ResponseCode.Base.ERROR);
                lApiResponse.setMsg(e.getMessage());
            } else if (e.getCause() instanceof TooManyResultsException) {
                lApiResponse = ApiResponse.creatFail(ResponseCode.Base.TOO_MANY_EXCEP);
            } else {
                lApiResponse.setCode(ResponseCode.Base.API_ERR);
                lApiResponse.setMsg("接口 [" + 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(), e.getMessage());
                } else {
                    message = e.getMessage();
                }
                logger.error(message, e);
            }
            responseResult(response, lApiResponse);
            return new ModelAndView();
        }
    });
}
Also used : HandlerExceptionResolver(org.springframework.web.servlet.HandlerExceptionResolver) TooManyResultsException(org.apache.ibatis.exceptions.TooManyResultsException) ModelAndView(org.springframework.web.servlet.ModelAndView) HttpServletResponse(javax.servlet.http.HttpServletResponse) TooManyResultsException(org.apache.ibatis.exceptions.TooManyResultsException) ServletException(javax.servlet.ServletException) ServiceException(com.rogge.common.core.ServiceException) IOException(java.io.IOException) NoHandlerFoundException(org.springframework.web.servlet.NoHandlerFoundException) ApiResponse(com.rogge.common.core.ApiResponse) HandlerMethod(org.springframework.web.method.HandlerMethod) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ServiceException(com.rogge.common.core.ServiceException) NoHandlerFoundException(org.springframework.web.servlet.NoHandlerFoundException)

Example 2 with ServiceException

use of com.rogge.common.core.ServiceException in project spring-cloud by Rogge666.

the class WebMvcConfigurer method configureHandlerExceptionResolvers.

// 统一异常处理
@Override
public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
    exceptionResolvers.add(new HandlerExceptionResolver() {

        public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception e) {
            ApiResponse lApiResponse = new ApiResponse();
            if (e instanceof ServiceException) {
                // 业务失败的异常,如“账号或密码错误”
                lApiResponse.setCode(ResponseCode.Base.ERROR);
                lApiResponse.setMsg(e.getMessage());
                logger.info(e.getMessage());
            } else if (e instanceof NoHandlerFoundException) {
                lApiResponse.setCode(ResponseCode.Base.API_NO_EXISTS);
                lApiResponse.setMsg("接口 [" + request.getRequestURI() + "] 不存在");
            } else if (e instanceof ServletException) {
                lApiResponse.setCode(ResponseCode.Base.ERROR);
                lApiResponse.setMsg(e.getMessage());
            } else if (e.getCause() instanceof TooManyResultsException) {
                lApiResponse = ApiResponse.creatFail(ResponseCode.Base.TOO_MANY_EXCEP);
            } else {
                lApiResponse.setCode(ResponseCode.Base.API_ERR);
                lApiResponse.setMsg("接口 [" + 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(), e.getMessage());
                } else {
                    message = e.getMessage();
                }
                logger.error(message, e);
            }
            responseResult(response, lApiResponse);
            return new ModelAndView();
        }
    });
}
Also used : HandlerExceptionResolver(org.springframework.web.servlet.HandlerExceptionResolver) TooManyResultsException(org.apache.ibatis.exceptions.TooManyResultsException) ModelAndView(org.springframework.web.servlet.ModelAndView) HttpServletResponse(javax.servlet.http.HttpServletResponse) TooManyResultsException(org.apache.ibatis.exceptions.TooManyResultsException) ServletException(javax.servlet.ServletException) ServiceException(com.rogge.common.core.ServiceException) IOException(java.io.IOException) NoHandlerFoundException(org.springframework.web.servlet.NoHandlerFoundException) ApiResponse(com.rogge.common.core.ApiResponse) HandlerMethod(org.springframework.web.method.HandlerMethod) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ServiceException(com.rogge.common.core.ServiceException) NoHandlerFoundException(org.springframework.web.servlet.NoHandlerFoundException)

Example 3 with ServiceException

use of com.rogge.common.core.ServiceException in project spring-cloud by Rogge666.

the class WebMvcConfigurer method configureHandlerExceptionResolvers.

// 统一异常处理
@Override
public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
    exceptionResolvers.add(new HandlerExceptionResolver() {

        public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception e) {
            ApiResponse lApiResponse = new ApiResponse();
            if (e instanceof ServiceException) {
                // 业务失败的异常,如“账号或密码错误”
                lApiResponse.setCode(ResponseCode.Base.ERROR);
                lApiResponse.setMsg(e.getMessage());
                logger.info(e.getMessage());
            } else if (e instanceof NoHandlerFoundException) {
                lApiResponse.setCode(ResponseCode.Base.API_NO_EXISTS);
                lApiResponse.setMsg("接口 [" + request.getRequestURI() + "] 不存在");
            } else if (e instanceof ServletException) {
                lApiResponse.setCode(ResponseCode.Base.ERROR);
                lApiResponse.setMsg(e.getMessage());
            } else if (e.getCause() instanceof TooManyResultsException) {
                lApiResponse = ApiResponse.creatFail(ResponseCode.Base.TOO_MANY_EXCEP);
            } else {
                lApiResponse.setCode(ResponseCode.Base.API_ERR);
                lApiResponse.setMsg("接口 [" + 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(), e.getMessage());
                } else {
                    message = e.getMessage();
                }
                logger.error(message, e);
            }
            responseResult(response, lApiResponse);
            return new ModelAndView();
        }
    });
}
Also used : HandlerExceptionResolver(org.springframework.web.servlet.HandlerExceptionResolver) TooManyResultsException(org.apache.ibatis.exceptions.TooManyResultsException) ModelAndView(org.springframework.web.servlet.ModelAndView) HttpServletResponse(javax.servlet.http.HttpServletResponse) TooManyResultsException(org.apache.ibatis.exceptions.TooManyResultsException) ServletException(javax.servlet.ServletException) ServiceException(com.rogge.common.core.ServiceException) IOException(java.io.IOException) NoHandlerFoundException(org.springframework.web.servlet.NoHandlerFoundException) ApiResponse(com.rogge.common.core.ApiResponse) HandlerMethod(org.springframework.web.method.HandlerMethod) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ServiceException(com.rogge.common.core.ServiceException) NoHandlerFoundException(org.springframework.web.servlet.NoHandlerFoundException)

Aggregations

ApiResponse (com.rogge.common.core.ApiResponse)3 ServiceException (com.rogge.common.core.ServiceException)3 IOException (java.io.IOException)3 ServletException (javax.servlet.ServletException)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 TooManyResultsException (org.apache.ibatis.exceptions.TooManyResultsException)3 HandlerMethod (org.springframework.web.method.HandlerMethod)3 HandlerExceptionResolver (org.springframework.web.servlet.HandlerExceptionResolver)3 ModelAndView (org.springframework.web.servlet.ModelAndView)3 NoHandlerFoundException (org.springframework.web.servlet.NoHandlerFoundException)3