Skip to content

Commit 3b29833

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 e065965 commit 3b29833

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
@@ -41,6 +41,7 @@
4141
import org.springframework.lang.NonNull;
4242
import org.springframework.util.CollectionUtils;
4343
import org.springframework.util.StreamUtils;
44+
import org.springframework.util.StringUtils;
4445
import org.springframework.web.client.ResourceAccessException;
4546
import org.springframework.web.client.ResponseErrorHandler;
4647

@@ -52,6 +53,7 @@
5253
* @author Christian Tzolov
5354
* @author SriVarshan P
5455
* @author Seunggyu Lee
56+
* @author Yanming Zhou
5557
*/
5658
@AutoConfiguration
5759
@ConditionalOnClass(RetryUtils.class)
@@ -104,12 +106,9 @@ public void handleError(@NonNull URI url, @NonNull HttpMethod method, @NonNull C
104106

105107
@SuppressWarnings("removal")
106108
public void handleError(@NonNull ClientHttpResponse response) throws IOException {
107-
if (!response.getStatusCode().isError()) {
108-
return;
109-
}
110109

111110
String error = StreamUtils.copyToString(response.getBody(), StandardCharsets.UTF_8);
112-
if (error == null || error.isEmpty()) {
111+
if (!StringUtils.hasLength(error)) {
113112
error = "No response body available";
114113
}
115114

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
@@ -44,6 +44,7 @@
4444
*
4545
* @author Christian Tzolov
4646
* @author Soby Chacko
47+
* @author Yanming Zhou
4748
* @since 0.8.1
4849
*/
4950
public abstract class RetryUtils {
@@ -78,20 +79,18 @@ public void handleError(final URI url, final HttpMethod method, final @NonNull C
7879

7980
@SuppressWarnings("removal")
8081
public void handleError(final @NonNull ClientHttpResponse response) throws IOException {
81-
if (response.getStatusCode().isError()) {
82-
String error = StreamUtils.copyToString(response.getBody(), StandardCharsets.UTF_8);
83-
String message = String.format("%s - %s", response.getStatusCode().value(), error);
84-
/*
85-
* Thrown on 4xx client errors, such as 401 - Incorrect API key provided,
86-
* 401 - You must be a member of an organization to use the API, 429 -
87-
* Rate limit reached for requests, 429 - You exceeded your current quota,
88-
* please check your plan and billing details.
89-
*/
90-
if (response.getStatusCode().is4xxClientError()) {
91-
throw new NonTransientAiException(message);
92-
}
93-
throw new TransientAiException(message);
82+
String error = StreamUtils.copyToString(response.getBody(), StandardCharsets.UTF_8);
83+
String message = String.format("%s - %s", response.getStatusCode().value(), error);
84+
/*
85+
* Thrown on 4xx client errors, such as 401 - Incorrect API key provided, 401
86+
* - You must be a member of an organization to use the API, 429 - Rate limit
87+
* reached for requests, 429 - You exceeded your current quota, please check
88+
* your plan and billing details.
89+
*/
90+
if (response.getStatusCode().is4xxClientError()) {
91+
throw new NonTransientAiException(message);
9492
}
93+
throw new TransientAiException(message);
9594
}
9695

9796
};

0 commit comments

Comments
 (0)