diff --git a/auto-configurations/common/spring-ai-autoconfigure-retry/src/main/java/org/springframework/ai/retry/autoconfigure/SpringAiRetryAutoConfiguration.java b/auto-configurations/common/spring-ai-autoconfigure-retry/src/main/java/org/springframework/ai/retry/autoconfigure/SpringAiRetryAutoConfiguration.java index 912f068f142..c0c132240c3 100644 --- a/auto-configurations/common/spring-ai-autoconfigure-retry/src/main/java/org/springframework/ai/retry/autoconfigure/SpringAiRetryAutoConfiguration.java +++ b/auto-configurations/common/spring-ai-autoconfigure-retry/src/main/java/org/springframework/ai/retry/autoconfigure/SpringAiRetryAutoConfiguration.java @@ -41,6 +41,7 @@ import org.springframework.lang.NonNull; import org.springframework.util.CollectionUtils; import org.springframework.util.StreamUtils; +import org.springframework.util.StringUtils; import org.springframework.web.client.ResourceAccessException; import org.springframework.web.client.ResponseErrorHandler; @@ -52,6 +53,7 @@ * @author Christian Tzolov * @author SriVarshan P * @author Seunggyu Lee + * @author Yanming Zhou */ @AutoConfiguration @ConditionalOnClass(RetryUtils.class) @@ -104,12 +106,9 @@ public void handleError(@NonNull URI url, @NonNull HttpMethod method, @NonNull C @SuppressWarnings("removal") public void handleError(@NonNull ClientHttpResponse response) throws IOException { - if (!response.getStatusCode().isError()) { - return; - } String error = StreamUtils.copyToString(response.getBody(), StandardCharsets.UTF_8); - if (error == null || error.isEmpty()) { + if (!StringUtils.hasLength(error)) { error = "No response body available"; } diff --git a/spring-ai-retry/src/main/java/org/springframework/ai/retry/RetryUtils.java b/spring-ai-retry/src/main/java/org/springframework/ai/retry/RetryUtils.java index 4d2b7ecb1ec..26f44d83ec1 100644 --- a/spring-ai-retry/src/main/java/org/springframework/ai/retry/RetryUtils.java +++ b/spring-ai-retry/src/main/java/org/springframework/ai/retry/RetryUtils.java @@ -44,6 +44,7 @@ * * @author Christian Tzolov * @author Soby Chacko + * @author Yanming Zhou * @since 0.8.1 */ public abstract class RetryUtils { @@ -78,20 +79,18 @@ public void handleError(final URI url, final HttpMethod method, final @NonNull C @SuppressWarnings("removal") public void handleError(final @NonNull ClientHttpResponse response) throws IOException { - if (response.getStatusCode().isError()) { - String error = StreamUtils.copyToString(response.getBody(), StandardCharsets.UTF_8); - String message = String.format("%s - %s", response.getStatusCode().value(), error); - /* - * Thrown on 4xx client errors, such as 401 - Incorrect API key provided, - * 401 - You must be a member of an organization to use the API, 429 - - * Rate limit reached for requests, 429 - You exceeded your current quota, - * please check your plan and billing details. - */ - if (response.getStatusCode().is4xxClientError()) { - throw new NonTransientAiException(message); - } - throw new TransientAiException(message); + String error = StreamUtils.copyToString(response.getBody(), StandardCharsets.UTF_8); + String message = String.format("%s - %s", response.getStatusCode().value(), error); + /* + * Thrown on 4xx client errors, such as 401 - Incorrect API key provided, 401 + * - You must be a member of an organization to use the API, 429 - Rate limit + * reached for requests, 429 - You exceeded your current quota, please check + * your plan and billing details. + */ + if (response.getStatusCode().is4xxClientError()) { + throw new NonTransientAiException(message); } + throw new TransientAiException(message); } };