From ddc7d4b1b5362b3aaada2f4cb690645d1169b07e Mon Sep 17 00:00:00 2001 From: Ilamparithi Natarajan <28389350+ilam-natarajan@users.noreply.github.com> Date: Fri, 5 Jan 2024 01:57:24 +0000 Subject: [PATCH] [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 --- .../libraries/resttemplate/ApiClient.mustache | 26 +++++++++++++------ .../org/openapitools/client/ApiClient.java | 26 +++++++++++++------ .../org/openapitools/client/ApiClient.java | 26 +++++++++++++------ .../org/openapitools/client/ApiClient.java | 26 +++++++++++++------ .../org/openapitools/client/ApiClient.java | 26 +++++++++++++------ .../org/openapitools/client/ApiClient.java | 26 +++++++++++++------ .../org/openapitools/client/ApiClient.java | 26 +++++++++++++------ .../org/openapitools/client/ApiClient.java | 26 +++++++++++++------ 8 files changed, 144 insertions(+), 64 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/ApiClient.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/ApiClient.mustache index 6ac032f4caa..f60c9acc266 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/ApiClient.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/ApiClient.mustache @@ -31,6 +31,7 @@ import org.springframework.util.CollectionUtils; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.util.StringUtils; +import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.HttpServerErrorException; import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestTemplate; @@ -772,20 +773,29 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { try { responseEntity = restTemplate.exchange(requestEntity, returnType); break; - } catch (HttpServerErrorException ex) { - attempts++; - if (attempts < maxAttemptsForRetry) { - try { - Thread.sleep(waitTimeMillis); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); + } catch (HttpServerErrorException | HttpClientErrorException ex) { + if (ex instanceof HttpServerErrorException + || ((HttpClientErrorException) ex) + .getStatusCode() + .equals(HttpStatus.TOO_MANY_REQUESTS)) { + attempts++; + if (attempts < maxAttemptsForRetry) { + try { + Thread.sleep(waitTimeMillis); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } else { + throw ex; } + } else { + throw ex; } } } if (responseEntity == null) { - throw new RestClientException("API returned HttpServerErrorException"); + throw new RestClientException("ResponseEntity is null"); } if (responseEntity.getStatusCode().is2xxSuccessful()) { diff --git a/samples/client/echo_api/java/resttemplate/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/echo_api/java/resttemplate/src/main/java/org/openapitools/client/ApiClient.java index a3598b82e19..b22902e928b 100644 --- a/samples/client/echo_api/java/resttemplate/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/echo_api/java/resttemplate/src/main/java/org/openapitools/client/ApiClient.java @@ -22,6 +22,7 @@ import org.springframework.util.CollectionUtils; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.util.StringUtils; +import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.HttpServerErrorException; import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestTemplate; @@ -688,20 +689,29 @@ public class ApiClient extends JavaTimeFormatter { try { responseEntity = restTemplate.exchange(requestEntity, returnType); break; - } catch (HttpServerErrorException ex) { - attempts++; - if (attempts < maxAttemptsForRetry) { - try { - Thread.sleep(waitTimeMillis); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); + } catch (HttpServerErrorException | HttpClientErrorException ex) { + if (ex instanceof HttpServerErrorException + || ((HttpClientErrorException) ex) + .getStatusCode() + .equals(HttpStatus.TOO_MANY_REQUESTS)) { + attempts++; + if (attempts < maxAttemptsForRetry) { + try { + Thread.sleep(waitTimeMillis); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } else { + throw ex; } + } else { + throw ex; } } } if (responseEntity == null) { - throw new RestClientException("API returned HttpServerErrorException"); + throw new RestClientException("ResponseEntity is null"); } if (responseEntity.getStatusCode().is2xxSuccessful()) { diff --git a/samples/client/others/java/resttemplate-useAbstractionForFiles/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/others/java/resttemplate-useAbstractionForFiles/src/main/java/org/openapitools/client/ApiClient.java index 04a52037010..58a39f5cba9 100644 --- a/samples/client/others/java/resttemplate-useAbstractionForFiles/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/others/java/resttemplate-useAbstractionForFiles/src/main/java/org/openapitools/client/ApiClient.java @@ -22,6 +22,7 @@ import org.springframework.util.CollectionUtils; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.util.StringUtils; +import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.HttpServerErrorException; import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestTemplate; @@ -631,20 +632,29 @@ public class ApiClient extends JavaTimeFormatter { try { responseEntity = restTemplate.exchange(requestEntity, returnType); break; - } catch (HttpServerErrorException ex) { - attempts++; - if (attempts < maxAttemptsForRetry) { - try { - Thread.sleep(waitTimeMillis); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); + } catch (HttpServerErrorException | HttpClientErrorException ex) { + if (ex instanceof HttpServerErrorException + || ((HttpClientErrorException) ex) + .getStatusCode() + .equals(HttpStatus.TOO_MANY_REQUESTS)) { + attempts++; + if (attempts < maxAttemptsForRetry) { + try { + Thread.sleep(waitTimeMillis); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } else { + throw ex; } + } else { + throw ex; } } } if (responseEntity == null) { - throw new RestClientException("API returned HttpServerErrorException"); + throw new RestClientException("ResponseEntity is null"); } if (responseEntity.getStatusCode().is2xxSuccessful()) { diff --git a/samples/client/petstore/java/resttemplate-jakarta/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/resttemplate-jakarta/src/main/java/org/openapitools/client/ApiClient.java index 6b160121d1e..86942b54bef 100644 --- a/samples/client/petstore/java/resttemplate-jakarta/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/resttemplate-jakarta/src/main/java/org/openapitools/client/ApiClient.java @@ -22,6 +22,7 @@ import org.springframework.util.CollectionUtils; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.util.StringUtils; +import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.HttpServerErrorException; import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestTemplate; @@ -680,20 +681,29 @@ public class ApiClient extends JavaTimeFormatter { try { responseEntity = restTemplate.exchange(requestEntity, returnType); break; - } catch (HttpServerErrorException ex) { - attempts++; - if (attempts < maxAttemptsForRetry) { - try { - Thread.sleep(waitTimeMillis); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); + } catch (HttpServerErrorException | HttpClientErrorException ex) { + if (ex instanceof HttpServerErrorException + || ((HttpClientErrorException) ex) + .getStatusCode() + .equals(HttpStatus.TOO_MANY_REQUESTS)) { + attempts++; + if (attempts < maxAttemptsForRetry) { + try { + Thread.sleep(waitTimeMillis); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } else { + throw ex; } + } else { + throw ex; } } } if (responseEntity == null) { - throw new RestClientException("API returned HttpServerErrorException"); + throw new RestClientException("ResponseEntity is null"); } if (responseEntity.getStatusCode().is2xxSuccessful()) { diff --git a/samples/client/petstore/java/resttemplate-swagger1/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/resttemplate-swagger1/src/main/java/org/openapitools/client/ApiClient.java index cfe6f43d754..178ce653c92 100644 --- a/samples/client/petstore/java/resttemplate-swagger1/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/resttemplate-swagger1/src/main/java/org/openapitools/client/ApiClient.java @@ -22,6 +22,7 @@ import org.springframework.util.CollectionUtils; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.util.StringUtils; +import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.HttpServerErrorException; import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestTemplate; @@ -680,20 +681,29 @@ public class ApiClient extends JavaTimeFormatter { try { responseEntity = restTemplate.exchange(requestEntity, returnType); break; - } catch (HttpServerErrorException ex) { - attempts++; - if (attempts < maxAttemptsForRetry) { - try { - Thread.sleep(waitTimeMillis); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); + } catch (HttpServerErrorException | HttpClientErrorException ex) { + if (ex instanceof HttpServerErrorException + || ((HttpClientErrorException) ex) + .getStatusCode() + .equals(HttpStatus.TOO_MANY_REQUESTS)) { + attempts++; + if (attempts < maxAttemptsForRetry) { + try { + Thread.sleep(waitTimeMillis); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } else { + throw ex; } + } else { + throw ex; } } } if (responseEntity == null) { - throw new RestClientException("API returned HttpServerErrorException"); + throw new RestClientException("ResponseEntity is null"); } if (responseEntity.getStatusCode().is2xxSuccessful()) { diff --git a/samples/client/petstore/java/resttemplate-swagger2/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/resttemplate-swagger2/src/main/java/org/openapitools/client/ApiClient.java index cfe6f43d754..178ce653c92 100644 --- a/samples/client/petstore/java/resttemplate-swagger2/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/resttemplate-swagger2/src/main/java/org/openapitools/client/ApiClient.java @@ -22,6 +22,7 @@ import org.springframework.util.CollectionUtils; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.util.StringUtils; +import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.HttpServerErrorException; import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestTemplate; @@ -680,20 +681,29 @@ public class ApiClient extends JavaTimeFormatter { try { responseEntity = restTemplate.exchange(requestEntity, returnType); break; - } catch (HttpServerErrorException ex) { - attempts++; - if (attempts < maxAttemptsForRetry) { - try { - Thread.sleep(waitTimeMillis); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); + } catch (HttpServerErrorException | HttpClientErrorException ex) { + if (ex instanceof HttpServerErrorException + || ((HttpClientErrorException) ex) + .getStatusCode() + .equals(HttpStatus.TOO_MANY_REQUESTS)) { + attempts++; + if (attempts < maxAttemptsForRetry) { + try { + Thread.sleep(waitTimeMillis); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } else { + throw ex; } + } else { + throw ex; } } } if (responseEntity == null) { - throw new RestClientException("API returned HttpServerErrorException"); + throw new RestClientException("ResponseEntity is null"); } if (responseEntity.getStatusCode().is2xxSuccessful()) { diff --git a/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/ApiClient.java index 053184ac6c6..60695abebf7 100644 --- a/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/ApiClient.java @@ -27,6 +27,7 @@ import org.springframework.util.CollectionUtils; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.util.StringUtils; +import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.HttpServerErrorException; import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestTemplate; @@ -743,20 +744,29 @@ public class ApiClient extends JavaTimeFormatter { try { responseEntity = restTemplate.exchange(requestEntity, returnType); break; - } catch (HttpServerErrorException ex) { - attempts++; - if (attempts < maxAttemptsForRetry) { - try { - Thread.sleep(waitTimeMillis); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); + } catch (HttpServerErrorException | HttpClientErrorException ex) { + if (ex instanceof HttpServerErrorException + || ((HttpClientErrorException) ex) + .getStatusCode() + .equals(HttpStatus.TOO_MANY_REQUESTS)) { + attempts++; + if (attempts < maxAttemptsForRetry) { + try { + Thread.sleep(waitTimeMillis); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } else { + throw ex; } + } else { + throw ex; } } } if (responseEntity == null) { - throw new RestClientException("API returned HttpServerErrorException"); + throw new RestClientException("ResponseEntity is null"); } if (responseEntity.getStatusCode().is2xxSuccessful()) { diff --git a/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/ApiClient.java index 02ac6820d01..a12083470ec 100644 --- a/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/ApiClient.java @@ -22,6 +22,7 @@ import org.springframework.util.CollectionUtils; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.util.StringUtils; +import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.HttpServerErrorException; import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestTemplate; @@ -738,20 +739,29 @@ public class ApiClient extends JavaTimeFormatter { try { responseEntity = restTemplate.exchange(requestEntity, returnType); break; - } catch (HttpServerErrorException ex) { - attempts++; - if (attempts < maxAttemptsForRetry) { - try { - Thread.sleep(waitTimeMillis); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); + } catch (HttpServerErrorException | HttpClientErrorException ex) { + if (ex instanceof HttpServerErrorException + || ((HttpClientErrorException) ex) + .getStatusCode() + .equals(HttpStatus.TOO_MANY_REQUESTS)) { + attempts++; + if (attempts < maxAttemptsForRetry) { + try { + Thread.sleep(waitTimeMillis); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } else { + throw ex; } + } else { + throw ex; } } } if (responseEntity == null) { - throw new RestClientException("API returned HttpServerErrorException"); + throw new RestClientException("ResponseEntity is null"); } if (responseEntity.getStatusCode().is2xxSuccessful()) {