Skip to content

Commit da9f262

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 da9f262

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.springframework.retry.support.RetryTemplate;
3939
import org.springframework.util.CollectionUtils;
4040
import org.springframework.util.StreamUtils;
41+
import org.springframework.util.StringUtils;
4142
import org.springframework.web.client.ResponseErrorHandler;
4243

4344
/**
@@ -47,6 +48,7 @@
4748
*
4849
* @author Christian Tzolov
4950
* @author SriVarshan P
51+
* @author Yanming Zhou
5052
*/
5153
@AutoConfiguration
5254
@ConditionalOnClass(RetryUtils.class)
@@ -88,12 +90,9 @@ public boolean hasError(@NonNull ClientHttpResponse response) throws IOException
8890

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

9594
String error = StreamUtils.copyToString(response.getBody(), StandardCharsets.UTF_8);
96-
if (error == null || error.isEmpty()) {
95+
if (!StringUtils.hasLength(error)) {
9796
error = "No response body available";
9897
}
9998

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)