Browse Source

ResponseErrorHandler provides access to URI and method

Issue: SPR-15511
pull/1325/merge
Rossen Stoyanchev 8 years ago
parent
commit
805fcc6a05
  1. 2
      spring-web/src/main/java/org/springframework/web/client/AsyncRestTemplate.java
  2. 15
      spring-web/src/main/java/org/springframework/web/client/ResponseErrorHandler.java
  3. 2
      spring-web/src/main/java/org/springframework/web/client/RestTemplate.java
  4. 6
      spring-web/src/test/java/org/springframework/web/client/RestTemplateTests.java

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

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

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

@ -17,7 +17,9 @@ @@ -17,7 +17,9 @@
package org.springframework.web.client;
import java.io.IOException;
import java.net.URI;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.ClientHttpResponse;
/**
@ -48,4 +50,17 @@ public interface ResponseErrorHandler { @@ -48,4 +50,17 @@ public interface ResponseErrorHandler {
*/
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);
}
}

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

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

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

@ -163,12 +163,14 @@ public class RestTemplateTests { @@ -163,12 +163,14 @@ public class RestTemplateTests {
@Test
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(errorHandler.hasError(response)).willReturn(true);
given(response.getStatusCode()).willReturn(HttpStatus.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 {
template.execute("http://example.com", HttpMethod.GET, null, null);

Loading…
Cancel
Save