Skip to content

Commit ab4952a

Browse files
committed
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
1 parent 0e3a1d8 commit ab4952a

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

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

+13-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,18 @@ public class DefaultResponseErrorHandler implements ResponseErrorHandler {
4545
* Delegates to {@link #hasError(HttpStatus)} with the response status code.
4646
*/
4747
public boolean hasError(ClientHttpResponse response) throws IOException {
48-
return hasError(response.getStatusCode());
48+
return hasError(getStatusCode(response));
49+
}
50+
51+
private HttpStatus getStatusCode(ClientHttpResponse response) throws IOException {
52+
HttpStatus statusCode;
53+
try {
54+
statusCode = response.getStatusCode();
55+
}
56+
catch (IllegalArgumentException ex) {
57+
throw new RestClientException("Unknown status code [" + response.getRawStatusCode() + "]");
58+
}
59+
return statusCode;
4960
}
5061

5162
/**
@@ -69,7 +80,7 @@ protected boolean hasError(HttpStatus statusCode) {
6980
* and a {@link RestClientException} in other cases.
7081
*/
7182
public void handleError(ClientHttpResponse response) throws IOException {
72-
HttpStatus statusCode = response.getStatusCode();
83+
HttpStatus statusCode = getStatusCode(response);
7384
HttpHeaders headers = response.getHeaders();
7485
MediaType contentType = headers.getContentType();
7586
Charset charset = contentType != null ? contentType.getCharSet() : null;

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

+12
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,16 @@ public void handleErrorNullResponse() throws Exception {
124124

125125
verify(response);
126126
}
127+
128+
// SPR-9406
129+
130+
@Test(expected=RestClientException.class)
131+
public void unknownStatusCode() throws Exception {
132+
expect(response.getStatusCode()).andThrow(new IllegalArgumentException("No matching constant for 999"));
133+
expect(response.getRawStatusCode()).andReturn(999);
134+
135+
replay(response);
136+
137+
handler.handleError(response);
138+
}
127139
}

src/dist/changelog.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Changes in version 3.2 M2
77
-------------------------
88

99
* spring-test module now depends on junit:junit-dep
10-
10+
* raise RestClientException instead of IllegalArgumentException for unknown status codes
1111

1212
Changes in version 3.2 M1 (2012-05-28)
1313
--------------------------------------

0 commit comments

Comments
 (0)