Search in sources :

Example 1 with ApiResponse

use of com.rogge.common.core.ApiResponse 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 ApiResponse

use of com.rogge.common.core.ApiResponse 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 ApiResponse

use of com.rogge.common.core.ApiResponse 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 4 with ApiResponse

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

the class LoginInterceptor method preHandle.

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    // 未登录
    User user = sessionUserInfo.getCurrentSessionUser(User.class);
    if (user == null) {
        ApiResponse apiResponse = ApiResponse.creatFail(ResponseCode.LoginRegister.NOLOGIN);
        response.setContentType("application/json;charset=UTF-8");
        response.getWriter().print(JSON.toJSONString(apiResponse, SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteEnumUsingToString));
        return false;
    } else {
        sessionUserInfo.setSessionUser(user);
    }
    return true;
}
Also used : User(com.rogge.common.model.User) ApiResponse(com.rogge.common.core.ApiResponse)

Aggregations

ApiResponse (com.rogge.common.core.ApiResponse)4 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 User (com.rogge.common.model.User)1