GlobalExceptionHandler.java 1.4 KB

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