Skip to content

Commit 32c2854

Browse files
committed
Remove redundant check from ResponseErrorHandler::handleError
`handleError()` is called only if `hasError()` returns true. Signed-off-by: Yanming Zhou <zhouyanming@gmail.com>
1 parent e464266 commit 32c2854

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

auto-configurations/common/spring-ai-autoconfigure-retry/src/main/java/org/springframework/ai/retry/autoconfigure/SpringAiRetryAutoConfiguration.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.ai.retry.autoconfigure;
1818

1919
import java.io.IOException;
20+
import java.net.URI;
2021
import java.nio.charset.StandardCharsets;
2122

2223
import org.slf4j.Logger;
@@ -30,6 +31,7 @@
3031
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
3132
import org.springframework.boot.context.properties.EnableConfigurationProperties;
3233
import org.springframework.context.annotation.Bean;
34+
import org.springframework.http.HttpMethod;
3335
import org.springframework.http.client.ClientHttpResponse;
3436
import org.springframework.lang.NonNull;
3537
import org.springframework.retry.RetryCallback;
@@ -38,6 +40,7 @@
3840
import org.springframework.retry.support.RetryTemplate;
3941
import org.springframework.util.CollectionUtils;
4042
import org.springframework.util.StreamUtils;
43+
import org.springframework.util.StringUtils;
4144
import org.springframework.web.client.ResponseErrorHandler;
4245

4346
/**
@@ -47,6 +50,7 @@
4750
*
4851
* @author Christian Tzolov
4952
* @author SriVarshan P
53+
* @author Yanming Zhou
5054
*/
5155
@AutoConfiguration
5256
@ConditionalOnClass(RetryUtils.class)
@@ -88,12 +92,9 @@ public boolean hasError(@NonNull ClientHttpResponse response) throws IOException
8892

8993
@Override
9094
public void handleError(@NonNull ClientHttpResponse response) throws IOException {
91-
if (!response.getStatusCode().isError()) {
92-
return;
93-
}
9495

9596
String error = StreamUtils.copyToString(response.getBody(), StandardCharsets.UTF_8);
96-
if (error == null || error.isEmpty()) {
97+
if (!StringUtils.hasLength(error)) {
9798
error = "No response body available";
9899
}
99100

spring-ai-retry/src/main/java/org/springframework/ai/retry/RetryUtils.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
*
4242
* @author Christian Tzolov
4343
* @author Soby Chacko
44+
* @author Yanming Zhou
4445
* @since 0.8.1
4546
*/
4647
public abstract class RetryUtils {
@@ -60,20 +61,18 @@ public void handleError(URI url, HttpMethod method, @NonNull ClientHttpResponse
6061
@Override
6162
@SuppressWarnings("removal")
6263
public void handleError(@NonNull ClientHttpResponse response) throws IOException {
63-
if (response.getStatusCode().isError()) {
64-
String error = StreamUtils.copyToString(response.getBody(), StandardCharsets.UTF_8);
65-
String message = String.format("%s - %s", response.getStatusCode().value(), error);
66-
/**
67-
* Thrown on 4xx client errors, such as 401 - Incorrect API key provided,
68-
* 401 - You must be a member of an organization to use the API, 429 -
69-
* Rate limit reached for requests, 429 - You exceeded your current quota
70-
* , please check your plan and billing details.
71-
*/
72-
if (response.getStatusCode().is4xxClientError()) {
73-
throw new NonTransientAiException(message);
74-
}
75-
throw new TransientAiException(message);
64+
String error = StreamUtils.copyToString(response.getBody(), StandardCharsets.UTF_8);
65+
String message = String.format("%s - %s", response.getStatusCode().value(), error);
66+
/**
67+
* Thrown on 4xx client errors, such as 401 - Incorrect API key provided, 401
68+
* - You must be a member of an organization to use the API, 429 - Rate limit
69+
* reached for requests, 429 - You exceeded your current quota , please check
70+
* your plan and billing details.
71+
*/
72+
if (response.getStatusCode().is4xxClientError()) {
73+
throw new NonTransientAiException(message);
7674
}
75+
throw new TransientAiException(message);
7776
}
7877
};
7978

0 commit comments

Comments
 (0)