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 1d7eed38da9..374a0242e39 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 @@ -38,6 +38,7 @@ import org.springframework.retry.support.RetryTemplate; import org.springframework.util.CollectionUtils; import org.springframework.util.StreamUtils; +import org.springframework.util.StringUtils; import org.springframework.web.client.ResponseErrorHandler; /** @@ -47,6 +48,7 @@ * * @author Christian Tzolov * @author SriVarshan P + * @author Yanming Zhou */ @AutoConfiguration @ConditionalOnClass(RetryUtils.class) @@ -88,12 +90,9 @@ public boolean hasError(@NonNull ClientHttpResponse response) throws IOException @Override 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 3dc125fd2a8..a34219df236 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 @@ -41,6 +41,7 @@ * * @author Christian Tzolov * @author Soby Chacko + * @author Yanming Zhou * @since 0.8.1 */ public abstract class RetryUtils { @@ -60,20 +61,18 @@ public void handleError(URI url, HttpMethod method, @NonNull ClientHttpResponse @Override @SuppressWarnings("removal") public void handleError(@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); } };