ResponseErrorHandler provides access to URI and method

Issue: SPR-15511
This commit is contained in:
Rossen Stoyanchev 2017-05-24 15:34:40 -04:00
parent 9b53b86d24
commit 805fcc6a05
4 changed files with 21 additions and 4 deletions

View File

@ -539,7 +539,7 @@ public class AsyncRestTemplate extends org.springframework.http.client.support.I
// ignore // ignore
} }
} }
getErrorHandler().handleError(response); getErrorHandler().handleError(url, method, response);
} }
/** /**

View File

@ -17,7 +17,9 @@
package org.springframework.web.client; package org.springframework.web.client;
import java.io.IOException; import java.io.IOException;
import java.net.URI;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.ClientHttpResponse; import org.springframework.http.client.ClientHttpResponse;
/** /**
@ -48,4 +50,17 @@ public interface ResponseErrorHandler {
*/ */
void handleError(ClientHttpResponse response) throws IOException; void handleError(ClientHttpResponse response) throws IOException;
/**
* Alternative to {@link #handleError(ClientHttpResponse)} with extra
* information providing access to the request URL and HTTP method.
* @param url the request URL
* @param method the HTTP method
* @param response the response with the error
* @throws IOException in case of I/O errors
* @since 5.0
*/
default void handleError(URI url, HttpMethod method, ClientHttpResponse response) throws IOException {
handleError(response);
}
} }

View File

@ -726,7 +726,7 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
} }
} }
if (hasError) { if (hasError) {
errorHandler.handleError(response); errorHandler.handleError(url, method, response);
} }
} }

View File

@ -163,12 +163,14 @@ public class RestTemplateTests {
@Test @Test
public void errorHandling() throws Exception { public void errorHandling() throws Exception {
given(requestFactory.createRequest(new URI("http://example.com"), HttpMethod.GET)).willReturn(request); URI uri = new URI("http://example.com");
given(requestFactory.createRequest(uri, HttpMethod.GET)).willReturn(request);
given(request.execute()).willReturn(response); given(request.execute()).willReturn(response);
given(errorHandler.hasError(response)).willReturn(true); given(errorHandler.hasError(response)).willReturn(true);
given(response.getStatusCode()).willReturn(HttpStatus.INTERNAL_SERVER_ERROR); given(response.getStatusCode()).willReturn(HttpStatus.INTERNAL_SERVER_ERROR);
given(response.getStatusText()).willReturn("Internal Server Error"); given(response.getStatusText()).willReturn("Internal Server Error");
willThrow(new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR)).given(errorHandler).handleError(response); willThrow(new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR))
.given(errorHandler).handleError(uri, HttpMethod.GET, response);
try { try {
template.execute("http://example.com", HttpMethod.GET, null, null); template.execute("http://example.com", HttpMethod.GET, null, null);