GlobalException.java 1.2 KB

1234567891011121314151617181920212223242526272829303132
  1. package cn.cslg.pas.common;
  2. import cn.cslg.pas.common.core.exception.PermissionException;
  3. import cn.cslg.pas.common.utils.Response;
  4. import cn.cslg.pas.common.utils.ResponseEnum;
  5. import cn.dev33.satoken.exception.NotLoginException;
  6. import org.springframework.web.bind.annotation.ExceptionHandler;
  7. import org.springframework.web.bind.annotation.RestControllerAdvice;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10. /**
  11. * 全局异常处理
  12. */
  13. @RestControllerAdvice // 可指定包前缀,比如:(basePackages = "com.pj.admin")
  14. public class GlobalException {
  15. // 全局异常拦截(拦截项目中的所有异常)
  16. @ExceptionHandler
  17. public String handlerException(Exception e, HttpServletRequest request, HttpServletResponse response) throws Exception {
  18. // 打印堆栈,以供调试
  19. e.printStackTrace();
  20. if (e instanceof NotLoginException) {
  21. return Response.error(ResponseEnum.UNAUTHORIZED);
  22. } else if (e instanceof PermissionException) {
  23. return Response.error(ResponseEnum.NOT_PERMISSION);
  24. } else {
  25. return Response.error(ResponseEnum.SYSTEM_ERROR);
  26. }
  27. }
  28. }