Skip to content

Commit fa01e9c

Browse files
committedNov 29, 2024
Use response decorator to check if error handled
Closes gh-33980
1 parent 431d726 commit fa01e9c

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed
 

‎spring-web/src/main/java/org/springframework/web/client/DefaultResponseErrorHandler.java

+35-6
Original file line numberDiff line numberDiff line change
@@ -136,18 +136,29 @@ protected boolean hasError(int statusCode) {
136136
*/
137137
@Override
138138
public void handleError(URI url, HttpMethod method, ClientHttpResponse response) throws IOException {
139-
handleError(response);
139+
140+
// For backwards compatibility try handle(response) first
141+
HandleErrorResponseDecorator decorator = new HandleErrorResponseDecorator(response);
142+
handleError(decorator);
143+
if (decorator.isHandled()) {
144+
return;
145+
}
146+
140147
handleError(response, response.getStatusCode(), url, method);
141148
}
142149

143-
/**
144-
* {@inheritDoc}
145-
* <p>As of 6.2.1 this method is a no-op unless overridden.
146-
*/
147150
@SuppressWarnings("removal")
148151
@Override
149152
public void handleError(ClientHttpResponse response) throws IOException {
150-
// no-op, but here for backwards compatibility
153+
154+
// Called via handleError(url, method, response)
155+
if (response instanceof HandleErrorResponseDecorator decorator) {
156+
decorator.setNotHandled();
157+
return;
158+
}
159+
160+
// Called directly, so do handle
161+
handleError(response, response.getStatusCode(), null, null);
151162
}
152163

153164
/**
@@ -277,4 +288,22 @@ public InputStream getBody() {
277288
};
278289
}
279290

291+
292+
private static class HandleErrorResponseDecorator extends ClientHttpResponseDecorator {
293+
294+
private boolean handled = true;
295+
296+
public HandleErrorResponseDecorator(ClientHttpResponse delegate) {
297+
super(delegate);
298+
}
299+
300+
public void setNotHandled() {
301+
this.handled = false;
302+
}
303+
304+
public boolean isHandled() {
305+
return this.handled;
306+
}
307+
}
308+
280309
}

‎spring-web/src/main/java/org/springframework/web/client/ResponseErrorHandler.java

-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ default void handleError(URI url, HttpMethod method, ClientHttpResponse response
6565
*/
6666
@Deprecated(since = "6.2.1", forRemoval = true)
6767
default void handleError(ClientHttpResponse response) throws IOException {
68-
// no-op unless overridden
6968
}
7069

7170
}

0 commit comments

Comments
 (0)