11/*
2- * Copyright 2002-2022 the original author or authors.
2+ * Copyright 2002-2024 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
@@ -131,15 +131,17 @@ protected boolean hasError(int statusCode) {
131131 * {@link HttpStatus} enum range.
132132 * </ul>
133133 * @throws UnknownHttpStatusCodeException in case of an unresolvable status code
134- * @see #handleError(URI, HttpMethod, ClientHttpResponse, HttpStatusCode )
134+ * @see #handleError(ClientHttpResponse, HttpStatusCode, URI, HttpMethod )
135135 */
136136 @ Override
137137 public void handleError (ClientHttpResponse response ) throws IOException {
138- handleError (null , null , response );
138+ HttpStatusCode statusCode = response .getStatusCode ();
139+ handleError (response , statusCode , null , null );
139140 }
140141
141142 /**
142- * Handle the error in the given response with the given resolved status code.
143+ * Handle the error in the given response with the given resolved status code
144+ * and extra information providing access to the request URL and HTTP method.
143145 * <p>The default implementation throws:
144146 * <ul>
145147 * <li>{@link HttpClientErrorException} if the status code is in the 4xx
@@ -152,58 +154,51 @@ public void handleError(ClientHttpResponse response) throws IOException {
152154 * {@link HttpStatus} enum range.
153155 * </ul>
154156 * @throws UnknownHttpStatusCodeException in case of an unresolvable status code
155- * @see #handleError(URI, HttpMethod, ClientHttpResponse, HttpStatusCode)
157+ * @since 6.2
158+ * @see #handleError(ClientHttpResponse, HttpStatusCode, URI, HttpMethod)
156159 */
157160 @ Override
158161 public void handleError (URI url , HttpMethod method , ClientHttpResponse response ) throws IOException {
159162 HttpStatusCode statusCode = response .getStatusCode ();
160- handleError (url , method , response , statusCode );
163+ handleError (response , statusCode , url , method );
161164 }
162165
163166 /**
164167 * Return error message with details from the response body. For example:
165168 * <pre>
166- * 404 Not Found: [{'id': 123, 'message': 'my message'}]
169+ * 404 Not Found on GET request for "https://example.com" : [{'id': 123, 'message': 'my message'}]
167170 * </pre>
168171 */
169- private String getErrorMessage (
170- int rawStatusCode , String statusText , @ Nullable byte [] responseBody , @ Nullable Charset charset , @ Nullable URI url , @ Nullable HttpMethod method ) {
172+ private String getErrorMessage (int rawStatusCode , String statusText , @ Nullable byte [] responseBody , @ Nullable Charset charset ,
173+ @ Nullable URI url , @ Nullable HttpMethod method ) {
171174
172- String preface = getPreface (rawStatusCode , statusText , url , method );
175+ StringBuilder msg = new StringBuilder (rawStatusCode + " " + statusText );
176+ if (method != null ) {
177+ msg .append (" on " ).append (method ).append (" request" );
178+ }
179+ if (url != null ) {
180+ msg .append (" for \" " );
181+ String urlString = url .toString ();
182+ int idx = urlString .indexOf ('?' );
183+ if (idx != -1 ) {
184+ msg .append (urlString , 0 , idx );
185+ }
186+ else {
187+ msg .append (urlString );
188+ }
189+ msg .append ("\" " );
190+ }
191+ msg .append (": " );
173192 if (ObjectUtils .isEmpty (responseBody )) {
174- return preface + "[no body]" ;
193+ msg . append ( "[no body]" ) ;
175194 }
176-
177- charset = (charset != null ? charset : StandardCharsets .UTF_8 );
178-
179- String bodyText = new String (responseBody , charset );
180- bodyText = LogFormatUtils .formatValue (bodyText , -1 , true );
181-
182- return preface + bodyText ;
183- }
184-
185- private String getPreface (int rawStatusCode , String statusText , @ Nullable URI url , @ Nullable HttpMethod method ) {
186- StringBuilder preface = new StringBuilder (rawStatusCode + " " + statusText );
187- if (!ObjectUtils .isEmpty (method ) && !ObjectUtils .isEmpty (url )) {
188- preface .append (" after " ).append (method ).append (" " ).append (url ).append (" " );
195+ else {
196+ charset = (charset != null ? charset : StandardCharsets .UTF_8 );
197+ String bodyText = new String (responseBody , charset );
198+ bodyText = LogFormatUtils .formatValue (bodyText , -1 , true );
199+ msg .append (bodyText );
189200 }
190- preface .append (": " );
191- return preface .toString ();
192- }
193-
194- /**
195- * Handle the error based on the resolved status code.
196- *
197- * <p>The default implementation delegates to
198- * {@link HttpClientErrorException#create} for errors in the 4xx range, to
199- * {@link HttpServerErrorException#create} for errors in the 5xx range,
200- * or otherwise raises {@link UnknownHttpStatusCodeException}.
201- * @since 5.0
202- * @see HttpClientErrorException#create
203- * @see HttpServerErrorException#create
204- */
205- protected void handleError (ClientHttpResponse response , HttpStatusCode statusCode ) throws IOException {
206- handleError (null , null , response , statusCode );
201+ return msg .toString ();
207202 }
208203
209204 /**
@@ -213,12 +208,11 @@ protected void handleError(ClientHttpResponse response, HttpStatusCode statusCod
213208 * {@link HttpClientErrorException#create} for errors in the 4xx range, to
214209 * {@link HttpServerErrorException#create} for errors in the 5xx range,
215210 * or otherwise raises {@link UnknownHttpStatusCodeException}.
216- * @since 5.0
211+ * @since 6.2
217212 * @see HttpClientErrorException#create
218213 * @see HttpServerErrorException#create
219214 */
220- protected void handleError (@ Nullable URI url , @ Nullable HttpMethod method , ClientHttpResponse response ,
221- HttpStatusCode statusCode ) throws IOException {
215+ protected void handleError (ClientHttpResponse response , HttpStatusCode statusCode , @ Nullable URI url , @ Nullable HttpMethod method ) throws IOException {
222216 String statusText = response .getStatusText ();
223217 HttpHeaders headers = response .getHeaders ();
224218 byte [] body = getResponseBody (response );
0 commit comments