GlobalExceptionHandler.java 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. package com.nju.edu.pay.handler;
  2. import cn.dev33.satoken.exception.NotLoginException;
  3. import cn.dev33.satoken.exception.NotPermissionException;
  4. import cn.dev33.satoken.exception.SaTokenException;
  5. import com.seecoder.commoncore.util.Response;
  6. import org.springframework.http.HttpStatus;
  7. import org.springframework.web.bind.annotation.ExceptionHandler;
  8. import org.springframework.web.bind.annotation.ResponseStatus;
  9. import org.springframework.web.bind.annotation.RestControllerAdvice;
  10. @RestControllerAdvice
  11. public class GlobalExceptionHandler {
  12. @ExceptionHandler(SaTokenException.class)
  13. @ResponseStatus(HttpStatus.FORBIDDEN)
  14. public Response<String> handleException(SaTokenException ste){
  15. ste.printStackTrace();
  16. if(ste instanceof NotPermissionException){
  17. return Response.failed("NOT_PERMISSION",ste.getMessage());
  18. }
  19. if(ste instanceof NotLoginException){
  20. return Response.failed("NOT_LOGIN",ste.getMessage());
  21. }
  22. return Response.failed(ste.getMessage());
  23. }
  24. @ExceptionHandler(Exception.class)
  25. @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
  26. public Response<String> handleException(Exception e){
  27. e.printStackTrace();
  28. return Response.failed(e.getMessage());
  29. }
  30. }