Skip to content

Commit ab19c22

Browse files
committed
DefaultResponseErrorHandler.hasError tolerates unknown status codes
Issue: SPR-16108
1 parent be5b935 commit ab19c22

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@
2929
* Spring's default implementation of the {@link ResponseErrorHandler} interface.
3030
*
3131
* <p>This error handler checks for the status code on the {@link ClientHttpResponse}:
32-
* Any code with series {@link org.springframework.http.HttpStatus.Series#CLIENT_ERROR} or
33-
* {@link org.springframework.http.HttpStatus.Series#SERVER_ERROR} is considered to be an
34-
* error. This behavior can be changed by overriding the {@link #hasError(HttpStatus)} method.
32+
* Any code with series {@link org.springframework.http.HttpStatus.Series#CLIENT_ERROR}
33+
* or {@link org.springframework.http.HttpStatus.Series#SERVER_ERROR} is considered to be
34+
* an error; this behavior can be changed by overriding the {@link #hasError(HttpStatus)}
35+
* method. Unknown status codes will be ignored by {@link #hasError(ClientHttpResponse)}.
3536
*
3637
* @author Arjen Poutsma
3738
* @author Rossen Stoyanchev
39+
* @author Juergen Hoeller
3840
* @since 3.0
3941
* @see RestTemplate#setErrorHandler
4042
*/
@@ -45,7 +47,12 @@ public class DefaultResponseErrorHandler implements ResponseErrorHandler {
4547
*/
4648
@Override
4749
public boolean hasError(ClientHttpResponse response) throws IOException {
48-
return hasError(getHttpStatusCode(response));
50+
try {
51+
return hasError(getHttpStatusCode(response));
52+
}
53+
catch (UnknownHttpStatusCodeException ex) {
54+
return false;
55+
}
4956
}
5057

5158
/**

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
* Unit tests for {@link DefaultResponseErrorHandler}.
3434
*
3535
* @author Arjen Poutsma
36+
* @author Juergen Hoeller
3637
*/
3738
public class DefaultResponseErrorHandlerTests {
3839

@@ -67,8 +68,8 @@ public void handleError() throws Exception {
6768
handler.handleError(response);
6869
fail("expected HttpClientErrorException");
6970
}
70-
catch (HttpClientErrorException e) {
71-
assertSame(headers, e.getResponseHeaders());
71+
catch (HttpClientErrorException ex) {
72+
assertSame(headers, ex.getResponseHeaders());
7273
}
7374
}
7475

@@ -103,11 +104,22 @@ public void unknownStatusCode() throws Exception {
103104
headers.setContentType(MediaType.TEXT_PLAIN);
104105

105106
given(response.getStatusCode()).willThrow(new IllegalArgumentException("No matching constant for 999"));
106-
given(response.getRawStatusCode()).willReturn(999);
107107
given(response.getStatusText()).willReturn("Custom status code");
108108
given(response.getHeaders()).willReturn(headers);
109109

110110
handler.handleError(response);
111111
}
112112

113+
@Test // SPR-16108
114+
public void hasErrorForUnknownStatusCode() throws Exception {
115+
HttpHeaders headers = new HttpHeaders();
116+
headers.setContentType(MediaType.TEXT_PLAIN);
117+
118+
given(response.getStatusCode()).willThrow(new IllegalArgumentException("No matching constant for 999"));
119+
given(response.getStatusText()).willReturn("Custom status code");
120+
given(response.getHeaders()).willReturn(headers);
121+
122+
assertFalse(handler.hasError(response));
123+
}
124+
113125
}

0 commit comments

Comments
 (0)