GlobalExceptionHandler.java 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. package com.fdkankan.agent.exception;
  2. import com.fdkankan.agent.common.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, "系统错误");
  21. }
  22. /**
  23. * 处理业务异常
  24. */
  25. @ResponseBody
  26. @ExceptionHandler(value = BusinessException.class)
  27. public ResultData businessExceptionHandler(BusinessException e) {
  28. log.error("业务异常code:{},message:{}", e.getCode(), e.getMessage());
  29. return ResultData.error(e.getCode(), e.getMessage(),e.getData());
  30. }
  31. }