GlobalExceptionHandler.java 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. package com.fdkankan.ucenter.exception;
  2. import com.fdkankan.common.exception.BusinessException;
  3. import com.fdkankan.ucenter.common.Result;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.springframework.web.bind.annotation.ExceptionHandler;
  6. import org.springframework.web.bind.annotation.ResponseBody;
  7. import org.springframework.web.bind.annotation.RestControllerAdvice;
  8. /**
  9. * 全局异常处理器
  10. */
  11. @RestControllerAdvice
  12. @Slf4j
  13. public class GlobalExceptionHandler {
  14. /**
  15. * 处理未知异常
  16. */
  17. @ResponseBody
  18. @ExceptionHandler(value = Exception.class)
  19. public Result exceptionHandler(Exception e) throws Exception {
  20. log.error("服务错误:", e);
  21. return Result.failure( 500, e.getMessage());
  22. }
  23. /**
  24. * 处理业务异常
  25. */
  26. @ResponseBody
  27. @ExceptionHandler(value = BusinessException.class)
  28. public Result businessExceptionHandler(BusinessException e) {
  29. log.info("业务异常code:{},message:{}", e.getCode(), e.getMessage());
  30. return Result.failure(e.getCode(), e.getMessage());
  31. }
  32. }