Browse Source

Raise RestClientException for unknown status codes

HttpStatus cannot be created with an unknown status code. If a server
returns a status code that's not in the HttpStatus enum values, an
IllegalArgumentException is raised. Rather than allowing it to
propagate as such, this change ensures the actual exception raised is
a RestClientException.

Issue: SPR-9406
pull/93/head
Rossen Stoyanchev 14 years ago
parent
commit
ab4952a959
  1. 15
      spring-web/src/main/java/org/springframework/web/client/DefaultResponseErrorHandler.java
  2. 12
      spring-web/src/test/java/org/springframework/web/client/DefaultResponseErrorHandlerTests.java
  3. 2
      src/dist/changelog.txt

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

@ -45,7 +45,18 @@ public class DefaultResponseErrorHandler implements ResponseErrorHandler { @@ -45,7 +45,18 @@ public class DefaultResponseErrorHandler implements ResponseErrorHandler {
* Delegates to {@link #hasError(HttpStatus)} with the response status code.
*/
public boolean hasError(ClientHttpResponse response) throws IOException {
return hasError(response.getStatusCode());
return hasError(getStatusCode(response));
}
private HttpStatus getStatusCode(ClientHttpResponse response) throws IOException {
HttpStatus statusCode;
try {
statusCode = response.getStatusCode();
}
catch (IllegalArgumentException ex) {
throw new RestClientException("Unknown status code [" + response.getRawStatusCode() + "]");
}
return statusCode;
}
/**
@ -69,7 +80,7 @@ public class DefaultResponseErrorHandler implements ResponseErrorHandler { @@ -69,7 +80,7 @@ public class DefaultResponseErrorHandler implements ResponseErrorHandler {
* and a {@link RestClientException} in other cases.
*/
public void handleError(ClientHttpResponse response) throws IOException {
HttpStatus statusCode = response.getStatusCode();
HttpStatus statusCode = getStatusCode(response);
HttpHeaders headers = response.getHeaders();
MediaType contentType = headers.getContentType();
Charset charset = contentType != null ? contentType.getCharSet() : null;

12
spring-web/src/test/java/org/springframework/web/client/DefaultResponseErrorHandlerTests.java

@ -124,4 +124,16 @@ public class DefaultResponseErrorHandlerTests { @@ -124,4 +124,16 @@ public class DefaultResponseErrorHandlerTests {
verify(response);
}
// SPR-9406
@Test(expected=RestClientException.class)
public void unknownStatusCode() throws Exception {
expect(response.getStatusCode()).andThrow(new IllegalArgumentException("No matching constant for 999"));
expect(response.getRawStatusCode()).andReturn(999);
replay(response);
handler.handleError(response);
}
}

2
src/dist/changelog.txt vendored

@ -7,7 +7,7 @@ Changes in version 3.2 M2 @@ -7,7 +7,7 @@ Changes in version 3.2 M2
-------------------------
* spring-test module now depends on junit:junit-dep
* raise RestClientException instead of IllegalArgumentException for unknown status codes
Changes in version 3.2 M1 (2012-05-28)
--------------------------------------

Loading…
Cancel
Save