package com.zy.acs.manager.common.exception; 
 | 
  
 | 
import com.zy.acs.manager.common.constant.Constants; 
 | 
import com.zy.acs.manager.common.utils.CommonUtil; 
 | 
import com.zy.acs.framework.common.R; 
 | 
import org.slf4j.Logger; 
 | 
import org.slf4j.LoggerFactory; 
 | 
import org.springframework.security.access.AccessDeniedException; 
 | 
import org.springframework.web.HttpRequestMethodNotSupportedException; 
 | 
import org.springframework.web.bind.annotation.ControllerAdvice; 
 | 
import org.springframework.web.bind.annotation.ExceptionHandler; 
 | 
import org.springframework.web.bind.annotation.ResponseBody; 
 | 
  
 | 
import javax.servlet.http.HttpServletResponse; 
 | 
  
 | 
/** 
 | 
 * 全局异常处理器 
 | 
 * 
 | 
 * @author vincent 
 | 
 * @since 2018-02-22 11:29:30 
 | 
 */ 
 | 
@ControllerAdvice 
 | 
public class GlobalExceptionHandler { 
 | 
    private final Logger logger = LoggerFactory.getLogger(getClass()); 
 | 
  
 | 
    @ResponseBody 
 | 
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class) 
 | 
    public R methodNotSupportedExceptionHandler(HttpRequestMethodNotSupportedException e, 
 | 
                                                HttpServletResponse response) { 
 | 
        CommonUtil.addCrossHeaders(response); 
 | 
        return R.error("请求方式不正确"); 
 | 
    } 
 | 
  
 | 
    @ResponseBody 
 | 
    @ExceptionHandler(AccessDeniedException.class) 
 | 
    public R accessDeniedExceptionHandler(AccessDeniedException e, HttpServletResponse response) { 
 | 
        CommonUtil.addCrossHeaders(response); 
 | 
        return R.parse(Constants.UNAUTHORIZED_CODE + "-" + Constants.UNAUTHORIZED_MSG); 
 | 
    } 
 | 
  
 | 
    @ResponseBody 
 | 
    @ExceptionHandler(BusinessException.class) 
 | 
    public R businessExceptionHandler(BusinessException e, HttpServletResponse response) { 
 | 
        CommonUtil.addCrossHeaders(response); 
 | 
        return R.error(e.getMessage()); 
 | 
    } 
 | 
  
 | 
    @ResponseBody 
 | 
    @ExceptionHandler(Throwable.class) 
 | 
    public R exceptionHandler(Throwable e, HttpServletResponse response) { 
 | 
        logger.error(e.getMessage(), e); 
 | 
        CommonUtil.addCrossHeaders(response); 
 | 
        return R.error(Constants.RESULT_ERROR_MSG); 
 | 
    } 
 | 
  
 | 
} 
 |