forked from loafle/openapi-generator-original
[java][resttemplate] rethrow original exception when retry limits exceeded (#17488)
in rest template, when the retry limits exceeded rethrow the original exception also add 429 (Too many requests) status code to the retry logic fix #17478
This commit is contained in:
parent
23bd5d413c
commit
ddc7d4b1b5
@ -31,6 +31,7 @@ import org.springframework.util.CollectionUtils;
|
|||||||
import org.springframework.util.LinkedMultiValueMap;
|
import org.springframework.util.LinkedMultiValueMap;
|
||||||
import org.springframework.util.MultiValueMap;
|
import org.springframework.util.MultiValueMap;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
import org.springframework.web.client.HttpClientErrorException;
|
||||||
import org.springframework.web.client.HttpServerErrorException;
|
import org.springframework.web.client.HttpServerErrorException;
|
||||||
import org.springframework.web.client.RestClientException;
|
import org.springframework.web.client.RestClientException;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
@ -772,20 +773,29 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
|
|||||||
try {
|
try {
|
||||||
responseEntity = restTemplate.exchange(requestEntity, returnType);
|
responseEntity = restTemplate.exchange(requestEntity, returnType);
|
||||||
break;
|
break;
|
||||||
} catch (HttpServerErrorException ex) {
|
} catch (HttpServerErrorException | HttpClientErrorException ex) {
|
||||||
attempts++;
|
if (ex instanceof HttpServerErrorException
|
||||||
if (attempts < maxAttemptsForRetry) {
|
|| ((HttpClientErrorException) ex)
|
||||||
try {
|
.getStatusCode()
|
||||||
Thread.sleep(waitTimeMillis);
|
.equals(HttpStatus.TOO_MANY_REQUESTS)) {
|
||||||
} catch (InterruptedException e) {
|
attempts++;
|
||||||
Thread.currentThread().interrupt();
|
if (attempts < maxAttemptsForRetry) {
|
||||||
|
try {
|
||||||
|
Thread.sleep(waitTimeMillis);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw ex;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
throw ex;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (responseEntity == null) {
|
if (responseEntity == null) {
|
||||||
throw new RestClientException("API returned HttpServerErrorException");
|
throw new RestClientException("ResponseEntity is null");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (responseEntity.getStatusCode().is2xxSuccessful()) {
|
if (responseEntity.getStatusCode().is2xxSuccessful()) {
|
||||||
|
@ -22,6 +22,7 @@ import org.springframework.util.CollectionUtils;
|
|||||||
import org.springframework.util.LinkedMultiValueMap;
|
import org.springframework.util.LinkedMultiValueMap;
|
||||||
import org.springframework.util.MultiValueMap;
|
import org.springframework.util.MultiValueMap;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
import org.springframework.web.client.HttpClientErrorException;
|
||||||
import org.springframework.web.client.HttpServerErrorException;
|
import org.springframework.web.client.HttpServerErrorException;
|
||||||
import org.springframework.web.client.RestClientException;
|
import org.springframework.web.client.RestClientException;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
@ -688,20 +689,29 @@ public class ApiClient extends JavaTimeFormatter {
|
|||||||
try {
|
try {
|
||||||
responseEntity = restTemplate.exchange(requestEntity, returnType);
|
responseEntity = restTemplate.exchange(requestEntity, returnType);
|
||||||
break;
|
break;
|
||||||
} catch (HttpServerErrorException ex) {
|
} catch (HttpServerErrorException | HttpClientErrorException ex) {
|
||||||
attempts++;
|
if (ex instanceof HttpServerErrorException
|
||||||
if (attempts < maxAttemptsForRetry) {
|
|| ((HttpClientErrorException) ex)
|
||||||
try {
|
.getStatusCode()
|
||||||
Thread.sleep(waitTimeMillis);
|
.equals(HttpStatus.TOO_MANY_REQUESTS)) {
|
||||||
} catch (InterruptedException e) {
|
attempts++;
|
||||||
Thread.currentThread().interrupt();
|
if (attempts < maxAttemptsForRetry) {
|
||||||
|
try {
|
||||||
|
Thread.sleep(waitTimeMillis);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw ex;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
throw ex;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (responseEntity == null) {
|
if (responseEntity == null) {
|
||||||
throw new RestClientException("API returned HttpServerErrorException");
|
throw new RestClientException("ResponseEntity is null");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (responseEntity.getStatusCode().is2xxSuccessful()) {
|
if (responseEntity.getStatusCode().is2xxSuccessful()) {
|
||||||
|
@ -22,6 +22,7 @@ import org.springframework.util.CollectionUtils;
|
|||||||
import org.springframework.util.LinkedMultiValueMap;
|
import org.springframework.util.LinkedMultiValueMap;
|
||||||
import org.springframework.util.MultiValueMap;
|
import org.springframework.util.MultiValueMap;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
import org.springframework.web.client.HttpClientErrorException;
|
||||||
import org.springframework.web.client.HttpServerErrorException;
|
import org.springframework.web.client.HttpServerErrorException;
|
||||||
import org.springframework.web.client.RestClientException;
|
import org.springframework.web.client.RestClientException;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
@ -631,20 +632,29 @@ public class ApiClient extends JavaTimeFormatter {
|
|||||||
try {
|
try {
|
||||||
responseEntity = restTemplate.exchange(requestEntity, returnType);
|
responseEntity = restTemplate.exchange(requestEntity, returnType);
|
||||||
break;
|
break;
|
||||||
} catch (HttpServerErrorException ex) {
|
} catch (HttpServerErrorException | HttpClientErrorException ex) {
|
||||||
attempts++;
|
if (ex instanceof HttpServerErrorException
|
||||||
if (attempts < maxAttemptsForRetry) {
|
|| ((HttpClientErrorException) ex)
|
||||||
try {
|
.getStatusCode()
|
||||||
Thread.sleep(waitTimeMillis);
|
.equals(HttpStatus.TOO_MANY_REQUESTS)) {
|
||||||
} catch (InterruptedException e) {
|
attempts++;
|
||||||
Thread.currentThread().interrupt();
|
if (attempts < maxAttemptsForRetry) {
|
||||||
|
try {
|
||||||
|
Thread.sleep(waitTimeMillis);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw ex;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
throw ex;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (responseEntity == null) {
|
if (responseEntity == null) {
|
||||||
throw new RestClientException("API returned HttpServerErrorException");
|
throw new RestClientException("ResponseEntity is null");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (responseEntity.getStatusCode().is2xxSuccessful()) {
|
if (responseEntity.getStatusCode().is2xxSuccessful()) {
|
||||||
|
@ -22,6 +22,7 @@ import org.springframework.util.CollectionUtils;
|
|||||||
import org.springframework.util.LinkedMultiValueMap;
|
import org.springframework.util.LinkedMultiValueMap;
|
||||||
import org.springframework.util.MultiValueMap;
|
import org.springframework.util.MultiValueMap;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
import org.springframework.web.client.HttpClientErrorException;
|
||||||
import org.springframework.web.client.HttpServerErrorException;
|
import org.springframework.web.client.HttpServerErrorException;
|
||||||
import org.springframework.web.client.RestClientException;
|
import org.springframework.web.client.RestClientException;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
@ -680,20 +681,29 @@ public class ApiClient extends JavaTimeFormatter {
|
|||||||
try {
|
try {
|
||||||
responseEntity = restTemplate.exchange(requestEntity, returnType);
|
responseEntity = restTemplate.exchange(requestEntity, returnType);
|
||||||
break;
|
break;
|
||||||
} catch (HttpServerErrorException ex) {
|
} catch (HttpServerErrorException | HttpClientErrorException ex) {
|
||||||
attempts++;
|
if (ex instanceof HttpServerErrorException
|
||||||
if (attempts < maxAttemptsForRetry) {
|
|| ((HttpClientErrorException) ex)
|
||||||
try {
|
.getStatusCode()
|
||||||
Thread.sleep(waitTimeMillis);
|
.equals(HttpStatus.TOO_MANY_REQUESTS)) {
|
||||||
} catch (InterruptedException e) {
|
attempts++;
|
||||||
Thread.currentThread().interrupt();
|
if (attempts < maxAttemptsForRetry) {
|
||||||
|
try {
|
||||||
|
Thread.sleep(waitTimeMillis);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw ex;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
throw ex;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (responseEntity == null) {
|
if (responseEntity == null) {
|
||||||
throw new RestClientException("API returned HttpServerErrorException");
|
throw new RestClientException("ResponseEntity is null");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (responseEntity.getStatusCode().is2xxSuccessful()) {
|
if (responseEntity.getStatusCode().is2xxSuccessful()) {
|
||||||
|
@ -22,6 +22,7 @@ import org.springframework.util.CollectionUtils;
|
|||||||
import org.springframework.util.LinkedMultiValueMap;
|
import org.springframework.util.LinkedMultiValueMap;
|
||||||
import org.springframework.util.MultiValueMap;
|
import org.springframework.util.MultiValueMap;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
import org.springframework.web.client.HttpClientErrorException;
|
||||||
import org.springframework.web.client.HttpServerErrorException;
|
import org.springframework.web.client.HttpServerErrorException;
|
||||||
import org.springframework.web.client.RestClientException;
|
import org.springframework.web.client.RestClientException;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
@ -680,20 +681,29 @@ public class ApiClient extends JavaTimeFormatter {
|
|||||||
try {
|
try {
|
||||||
responseEntity = restTemplate.exchange(requestEntity, returnType);
|
responseEntity = restTemplate.exchange(requestEntity, returnType);
|
||||||
break;
|
break;
|
||||||
} catch (HttpServerErrorException ex) {
|
} catch (HttpServerErrorException | HttpClientErrorException ex) {
|
||||||
attempts++;
|
if (ex instanceof HttpServerErrorException
|
||||||
if (attempts < maxAttemptsForRetry) {
|
|| ((HttpClientErrorException) ex)
|
||||||
try {
|
.getStatusCode()
|
||||||
Thread.sleep(waitTimeMillis);
|
.equals(HttpStatus.TOO_MANY_REQUESTS)) {
|
||||||
} catch (InterruptedException e) {
|
attempts++;
|
||||||
Thread.currentThread().interrupt();
|
if (attempts < maxAttemptsForRetry) {
|
||||||
|
try {
|
||||||
|
Thread.sleep(waitTimeMillis);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw ex;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
throw ex;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (responseEntity == null) {
|
if (responseEntity == null) {
|
||||||
throw new RestClientException("API returned HttpServerErrorException");
|
throw new RestClientException("ResponseEntity is null");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (responseEntity.getStatusCode().is2xxSuccessful()) {
|
if (responseEntity.getStatusCode().is2xxSuccessful()) {
|
||||||
|
@ -22,6 +22,7 @@ import org.springframework.util.CollectionUtils;
|
|||||||
import org.springframework.util.LinkedMultiValueMap;
|
import org.springframework.util.LinkedMultiValueMap;
|
||||||
import org.springframework.util.MultiValueMap;
|
import org.springframework.util.MultiValueMap;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
import org.springframework.web.client.HttpClientErrorException;
|
||||||
import org.springframework.web.client.HttpServerErrorException;
|
import org.springframework.web.client.HttpServerErrorException;
|
||||||
import org.springframework.web.client.RestClientException;
|
import org.springframework.web.client.RestClientException;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
@ -680,20 +681,29 @@ public class ApiClient extends JavaTimeFormatter {
|
|||||||
try {
|
try {
|
||||||
responseEntity = restTemplate.exchange(requestEntity, returnType);
|
responseEntity = restTemplate.exchange(requestEntity, returnType);
|
||||||
break;
|
break;
|
||||||
} catch (HttpServerErrorException ex) {
|
} catch (HttpServerErrorException | HttpClientErrorException ex) {
|
||||||
attempts++;
|
if (ex instanceof HttpServerErrorException
|
||||||
if (attempts < maxAttemptsForRetry) {
|
|| ((HttpClientErrorException) ex)
|
||||||
try {
|
.getStatusCode()
|
||||||
Thread.sleep(waitTimeMillis);
|
.equals(HttpStatus.TOO_MANY_REQUESTS)) {
|
||||||
} catch (InterruptedException e) {
|
attempts++;
|
||||||
Thread.currentThread().interrupt();
|
if (attempts < maxAttemptsForRetry) {
|
||||||
|
try {
|
||||||
|
Thread.sleep(waitTimeMillis);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw ex;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
throw ex;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (responseEntity == null) {
|
if (responseEntity == null) {
|
||||||
throw new RestClientException("API returned HttpServerErrorException");
|
throw new RestClientException("ResponseEntity is null");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (responseEntity.getStatusCode().is2xxSuccessful()) {
|
if (responseEntity.getStatusCode().is2xxSuccessful()) {
|
||||||
|
@ -27,6 +27,7 @@ import org.springframework.util.CollectionUtils;
|
|||||||
import org.springframework.util.LinkedMultiValueMap;
|
import org.springframework.util.LinkedMultiValueMap;
|
||||||
import org.springframework.util.MultiValueMap;
|
import org.springframework.util.MultiValueMap;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
import org.springframework.web.client.HttpClientErrorException;
|
||||||
import org.springframework.web.client.HttpServerErrorException;
|
import org.springframework.web.client.HttpServerErrorException;
|
||||||
import org.springframework.web.client.RestClientException;
|
import org.springframework.web.client.RestClientException;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
@ -743,20 +744,29 @@ public class ApiClient extends JavaTimeFormatter {
|
|||||||
try {
|
try {
|
||||||
responseEntity = restTemplate.exchange(requestEntity, returnType);
|
responseEntity = restTemplate.exchange(requestEntity, returnType);
|
||||||
break;
|
break;
|
||||||
} catch (HttpServerErrorException ex) {
|
} catch (HttpServerErrorException | HttpClientErrorException ex) {
|
||||||
attempts++;
|
if (ex instanceof HttpServerErrorException
|
||||||
if (attempts < maxAttemptsForRetry) {
|
|| ((HttpClientErrorException) ex)
|
||||||
try {
|
.getStatusCode()
|
||||||
Thread.sleep(waitTimeMillis);
|
.equals(HttpStatus.TOO_MANY_REQUESTS)) {
|
||||||
} catch (InterruptedException e) {
|
attempts++;
|
||||||
Thread.currentThread().interrupt();
|
if (attempts < maxAttemptsForRetry) {
|
||||||
|
try {
|
||||||
|
Thread.sleep(waitTimeMillis);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw ex;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
throw ex;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (responseEntity == null) {
|
if (responseEntity == null) {
|
||||||
throw new RestClientException("API returned HttpServerErrorException");
|
throw new RestClientException("ResponseEntity is null");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (responseEntity.getStatusCode().is2xxSuccessful()) {
|
if (responseEntity.getStatusCode().is2xxSuccessful()) {
|
||||||
|
@ -22,6 +22,7 @@ import org.springframework.util.CollectionUtils;
|
|||||||
import org.springframework.util.LinkedMultiValueMap;
|
import org.springframework.util.LinkedMultiValueMap;
|
||||||
import org.springframework.util.MultiValueMap;
|
import org.springframework.util.MultiValueMap;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
import org.springframework.web.client.HttpClientErrorException;
|
||||||
import org.springframework.web.client.HttpServerErrorException;
|
import org.springframework.web.client.HttpServerErrorException;
|
||||||
import org.springframework.web.client.RestClientException;
|
import org.springframework.web.client.RestClientException;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
@ -738,20 +739,29 @@ public class ApiClient extends JavaTimeFormatter {
|
|||||||
try {
|
try {
|
||||||
responseEntity = restTemplate.exchange(requestEntity, returnType);
|
responseEntity = restTemplate.exchange(requestEntity, returnType);
|
||||||
break;
|
break;
|
||||||
} catch (HttpServerErrorException ex) {
|
} catch (HttpServerErrorException | HttpClientErrorException ex) {
|
||||||
attempts++;
|
if (ex instanceof HttpServerErrorException
|
||||||
if (attempts < maxAttemptsForRetry) {
|
|| ((HttpClientErrorException) ex)
|
||||||
try {
|
.getStatusCode()
|
||||||
Thread.sleep(waitTimeMillis);
|
.equals(HttpStatus.TOO_MANY_REQUESTS)) {
|
||||||
} catch (InterruptedException e) {
|
attempts++;
|
||||||
Thread.currentThread().interrupt();
|
if (attempts < maxAttemptsForRetry) {
|
||||||
|
try {
|
||||||
|
Thread.sleep(waitTimeMillis);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw ex;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
throw ex;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (responseEntity == null) {
|
if (responseEntity == null) {
|
||||||
throw new RestClientException("API returned HttpServerErrorException");
|
throw new RestClientException("ResponseEntity is null");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (responseEntity.getStatusCode().is2xxSuccessful()) {
|
if (responseEntity.getStatusCode().is2xxSuccessful()) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user