content: package com.xiliu.server.common.exception.handler;
  
  
  import com.xiliu.common.result.R;
  import com.xiliu.common.utils.StringUtils;
  import com.xiliu.server.common.exception.ServiceException;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.web.bind.annotation.ExceptionHandler;
  import org.springframework.web.bind.annotation.RestControllerAdvice;
  
  import javax.servlet.http.HttpServletRequest;
  
  /**
   * @author
   * @description 全局异常处理
   * @date 2023/3/9
   */
  @RestControllerAdvice
  public class GlobalExceptionHandler
  {
      private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
  
  
      /**
       * 业务异常
       */
      @ExceptionHandler(ServiceException.class)
      public R handleServiceException(ServiceException e, HttpServletRequest request) {
          log.error(e.getMessage(), e);
          Integer code = e.getCode();
          return StringUtils.isNotNull(code) ? R.error(code, e.getMessage()) : R.error(e.getMessage());
      }
  
      /**
       * 拦截未知的运行时异常
       */
      @ExceptionHandler(RuntimeException.class)
      public R handleRuntimeException(RuntimeException e, HttpServletRequest request) {
          String requestUrl = request.getRequestURI();
          log.error("请求地址'{}',发生未知异常.", requestUrl, e);
          return R.error("系统发生未知异常，请联系管理员");
      }
  
      /**
       * 系统异常
       */
      @ExceptionHandler(Exception.class)
      public R handleException(Exception e, HttpServletRequest request) {
          String requestUrl = request.getRequestURI();
          log.error("请求地址'{}',发生系统异常.", requestUrl, e);
          return R.error("系统出现内部错误，请联系管理员");
      }
  }
  