1
1
/*
2
- * Copyright 2002-2022 the original author or authors.
2
+ * Copyright 2002-2024 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -131,15 +131,17 @@ protected boolean hasError(int statusCode) {
131
131
* {@link HttpStatus} enum range.
132
132
* </ul>
133
133
* @throws UnknownHttpStatusCodeException in case of an unresolvable status code
134
- * @see #handleError(URI, HttpMethod, ClientHttpResponse, HttpStatusCode )
134
+ * @see #handleError(ClientHttpResponse, HttpStatusCode, URI, HttpMethod )
135
135
*/
136
136
@ Override
137
137
public void handleError (ClientHttpResponse response ) throws IOException {
138
- handleError (null , null , response );
138
+ HttpStatusCode statusCode = response .getStatusCode ();
139
+ handleError (response , statusCode , null , null );
139
140
}
140
141
141
142
/**
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.
143
145
* <p>The default implementation throws:
144
146
* <ul>
145
147
* <li>{@link HttpClientErrorException} if the status code is in the 4xx
@@ -152,58 +154,51 @@ public void handleError(ClientHttpResponse response) throws IOException {
152
154
* {@link HttpStatus} enum range.
153
155
* </ul>
154
156
* @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)
156
159
*/
157
160
@ Override
158
161
public void handleError (URI url , HttpMethod method , ClientHttpResponse response ) throws IOException {
159
162
HttpStatusCode statusCode = response .getStatusCode ();
160
- handleError (url , method , response , statusCode );
163
+ handleError (response , statusCode , url , method );
161
164
}
162
165
163
166
/**
164
167
* Return error message with details from the response body. For example:
165
168
* <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'}]
167
170
* </pre>
168
171
*/
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 ) {
171
174
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 (": " );
173
192
if (ObjectUtils .isEmpty (responseBody )) {
174
- return preface + "[no body]" ;
193
+ msg . append ( "[no body]" ) ;
175
194
}
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 );
189
200
}
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 ();
207
202
}
208
203
209
204
/**
@@ -213,12 +208,11 @@ protected void handleError(ClientHttpResponse response, HttpStatusCode statusCod
213
208
* {@link HttpClientErrorException#create} for errors in the 4xx range, to
214
209
* {@link HttpServerErrorException#create} for errors in the 5xx range,
215
210
* or otherwise raises {@link UnknownHttpStatusCodeException}.
216
- * @since 5.0
211
+ * @since 6.2
217
212
* @see HttpClientErrorException#create
218
213
* @see HttpServerErrorException#create
219
214
*/
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 {
222
216
String statusText = response .getStatusText ();
223
217
HttpHeaders headers = response .getHeaders ();
224
218
byte [] body = getResponseBody (response );
0 commit comments