@@ -155,33 +155,53 @@ public static BodyHandler<Void> discarding() {
155155 }
156156
157157 /**
158- * Throws on HTTP error, or apply the provided body handler.
158+ * Throws on HTTP error using the provided mapper , or apply the provided body handler.
159159 * @param handler the body handler to apply on non-error HTTP responses
160160 * @param isSuccess a callback determining error cases
161+ * @param exceptionMapper the exception mapper
161162 * @return the body handler
162163 * @param <T> the type of the body handler
163164 */
164165 public static <T > Response .BodyHandler <T > throwOnError (
165166 final Response .BodyHandler <T > handler ,
166- final Function <Response .ResponseInfo , Boolean > isSuccess
167+ final Function <Response .ResponseInfo , Boolean > isSuccess ,
168+ final Function <Response .ResponseInfo , ClientHttpException > exceptionMapper
167169 ) {
168170 return responseinfo -> {
169171 if (!isSuccess .apply (responseinfo )) {
170- throw new ClientHttpException (
171- "An HTTP error has been returned from "
172- + responseinfo .uri ()
173- + " with status code "
174- + responseinfo .statusCode (),
175- responseinfo .uri (),
176- responseinfo .statusCode (),
177- responseinfo .headers (),
178- new String (responseinfo .body ().array (), StandardCharsets .UTF_8 )
179- );
172+ throw exceptionMapper .apply (responseinfo );
180173 }
181174 return handler .apply (responseinfo );
182175 };
183176 }
184177
178+ /**
179+ * Throws on HTTP error, or apply the provided body handler.
180+ * @param handler the body handler to apply on non-error HTTP responses
181+ * @param isSuccess a callback determining error cases
182+ * @return the body handler
183+ * @param <T> the type of the body handler
184+ */
185+ public static <T > Response .BodyHandler <T > throwOnError (
186+ final Response .BodyHandler <T > handler ,
187+ final Function <Response .ResponseInfo , Boolean > isSuccess
188+ ) {
189+ final Function <Response .ResponseInfo , ClientHttpException > defaultMapper = responseInfo ->
190+ new ClientHttpException (
191+ "An HTTP error has been returned from "
192+ + responseInfo .uri ()
193+ + " with status code "
194+ + responseInfo .statusCode (),
195+ responseInfo .uri (),
196+ responseInfo .statusCode (),
197+ responseInfo .headers (),
198+ new String (responseInfo .body ().array (), StandardCharsets .UTF_8 )
199+ );
200+ return throwOnError (handler , isSuccess , defaultMapper );
201+ }
202+
203+
204+
185205 private BodyHandlers () {
186206 // Prevent instantiation
187207 }
0 commit comments