Skip to content

Commit 805fcc6

Browse files
committed
ResponseErrorHandler provides access to URI and method
Issue: SPR-15511
1 parent 9b53b86 commit 805fcc6

File tree

4 files changed

+21
-4
lines changed

4 files changed

+21
-4
lines changed

spring-web/src/main/java/org/springframework/web/client/AsyncRestTemplate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ private void handleResponseError(HttpMethod method, URI url, ClientHttpResponse
539539
// ignore
540540
}
541541
}
542-
getErrorHandler().handleError(response);
542+
getErrorHandler().handleError(url, method, response);
543543
}
544544

545545
/**

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
package org.springframework.web.client;
1818

1919
import java.io.IOException;
20+
import java.net.URI;
2021

22+
import org.springframework.http.HttpMethod;
2123
import org.springframework.http.client.ClientHttpResponse;
2224

2325
/**
@@ -48,4 +50,17 @@ public interface ResponseErrorHandler {
4850
*/
4951
void handleError(ClientHttpResponse response) throws IOException;
5052

53+
/**
54+
* Alternative to {@link #handleError(ClientHttpResponse)} with extra
55+
* information providing access to the request URL and HTTP method.
56+
* @param url the request URL
57+
* @param method the HTTP method
58+
* @param response the response with the error
59+
* @throws IOException in case of I/O errors
60+
* @since 5.0
61+
*/
62+
default void handleError(URI url, HttpMethod method, ClientHttpResponse response) throws IOException {
63+
handleError(response);
64+
}
65+
5166
}

spring-web/src/main/java/org/springframework/web/client/RestTemplate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ protected void handleResponse(URI url, HttpMethod method, ClientHttpResponse res
726726
}
727727
}
728728
if (hasError) {
729-
errorHandler.handleError(response);
729+
errorHandler.handleError(url, method, response);
730730
}
731731
}
732732

spring-web/src/test/java/org/springframework/web/client/RestTemplateTests.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,14 @@ public void uriTemplateWithTrailingSlash() throws Exception {
163163

164164
@Test
165165
public void errorHandling() throws Exception {
166-
given(requestFactory.createRequest(new URI("http://example.com"), HttpMethod.GET)).willReturn(request);
166+
URI uri = new URI("http://example.com");
167+
given(requestFactory.createRequest(uri, HttpMethod.GET)).willReturn(request);
167168
given(request.execute()).willReturn(response);
168169
given(errorHandler.hasError(response)).willReturn(true);
169170
given(response.getStatusCode()).willReturn(HttpStatus.INTERNAL_SERVER_ERROR);
170171
given(response.getStatusText()).willReturn("Internal Server Error");
171-
willThrow(new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR)).given(errorHandler).handleError(response);
172+
willThrow(new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR))
173+
.given(errorHandler).handleError(uri, HttpMethod.GET, response);
172174

173175
try {
174176
template.execute("http://example.com", HttpMethod.GET, null, null);

0 commit comments

Comments
 (0)