Skip to content

Commit f22d1ea

Browse files
committed
Add createException to ConvertibleClientHttpResponse
Closes gh-35391
1 parent 21e6d73 commit f22d1ea

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,11 @@ public void close() {
961961
Class<T> bodyClass = bodyClass(type);
962962
return readWithMessageConverters(this.delegate, () -> {}, type, bodyClass, this.hints);
963963
}
964+
965+
@Override
966+
public RestClientResponseException createException() throws IOException {
967+
return StatusHandler.createException(this, DefaultRestClient.this.messageConverters);
968+
}
964969
}
965970

966971
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,15 @@ interface ConvertibleClientHttpResponse extends ClientHttpResponse {
878878
* @return the body, or {@code null} if no response body was available
879879
*/
880880
<T> @Nullable T bodyTo(ParameterizedTypeReference<T> bodyType);
881+
882+
/**
883+
* Create a {@link RestClientResponseException} of the appropriate
884+
* subtype depending on the response status code. The exception contains
885+
* the status, headers, and body of the response.
886+
* @throws IOException in case of a response failure (e.g. to obtain the status)
887+
* @since 7.0
888+
*/
889+
RestClientResponseException createException() throws IOException;
881890
}
882891
}
883892

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,16 @@ public static StatusHandler createDefaultStatusHandler(List<HttpMessageConverter
108108
});
109109
}
110110

111-
private static RestClientResponseException createException(
111+
/**
112+
* Create a {@link RestClientResponseException} of the appropriate
113+
* subtype depending on the response status code.
114+
* @param response the response
115+
* @param converters the converters to use to decode the body
116+
* @return the created exception
117+
* @throws IOException in case of a response failure (e.g. to obtain the status)
118+
* @since 7.0
119+
*/
120+
public static RestClientResponseException createException(
112121
ClientHttpResponse response, List<HttpMessageConverter<?>> converters) throws IOException {
113122

114123
HttpStatusCode statusCode = response.getStatusCode();

0 commit comments

Comments
 (0)