CustomExceptionTranslator.java 987 B

12345678910111213141516171819202122232425
  1. package com.diagbot.config;
  2. import org.springframework.http.ResponseEntity;
  3. import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
  4. import org.springframework.security.oauth2.provider.error.DefaultWebResponseExceptionTranslator;
  5. /**
  6. * @Description:
  7. * @Author songxl
  8. * @Date 2021/11/22
  9. */
  10. public class CustomExceptionTranslator extends DefaultWebResponseExceptionTranslator {
  11. @Override
  12. public ResponseEntity<OAuth2Exception> translate(Exception e) throws Exception {
  13. ResponseEntity<OAuth2Exception> translate = super.translate(e);
  14. OAuth2Exception body = translate.getBody();
  15. CustomOauthException customOauthException = new CustomOauthException(body.getMessage(),body.getOAuth2ErrorCode(),
  16. body.getHttpErrorCode());
  17. ResponseEntity<OAuth2Exception> response = new ResponseEntity<>(customOauthException, translate.getHeaders(),
  18. translate.getStatusCode());
  19. return response;
  20. }
  21. }