19
19
import java .io .ByteArrayInputStream ;
20
20
import java .io .IOException ;
21
21
import java .io .InputStream ;
22
+ import java .net .URI ;
22
23
import java .nio .charset .Charset ;
23
24
import java .nio .charset .StandardCharsets ;
24
25
import java .util .Collections ;
28
29
import org .springframework .core .ResolvableType ;
29
30
import org .springframework .core .log .LogFormatUtils ;
30
31
import org .springframework .http .HttpHeaders ;
32
+ import org .springframework .http .HttpMethod ;
31
33
import org .springframework .http .HttpStatus ;
32
34
import org .springframework .http .HttpStatusCode ;
33
35
import org .springframework .http .MediaType ;
@@ -129,12 +131,33 @@ protected boolean hasError(int statusCode) {
129
131
* {@link HttpStatus} enum range.
130
132
* </ul>
131
133
* @throws UnknownHttpStatusCodeException in case of an unresolvable status code
132
- * @see #handleError(ClientHttpResponse, HttpStatusCode)
134
+ * @see #handleError(URI, HttpMethod, ClientHttpResponse, HttpStatusCode)
133
135
*/
134
136
@ Override
135
137
public void handleError (ClientHttpResponse response ) throws IOException {
138
+ handleError (null , null , response );
139
+ }
140
+
141
+ /**
142
+ * Handle the error in the given response with the given resolved status code.
143
+ * <p>The default implementation throws:
144
+ * <ul>
145
+ * <li>{@link HttpClientErrorException} if the status code is in the 4xx
146
+ * series, or one of its sub-classes such as
147
+ * {@link HttpClientErrorException.BadRequest} and others.
148
+ * <li>{@link HttpServerErrorException} if the status code is in the 5xx
149
+ * series, or one of its sub-classes such as
150
+ * {@link HttpServerErrorException.InternalServerError} and others.
151
+ * <li>{@link UnknownHttpStatusCodeException} for error status codes not in the
152
+ * {@link HttpStatus} enum range.
153
+ * </ul>
154
+ * @throws UnknownHttpStatusCodeException in case of an unresolvable status code
155
+ * @see #handleError(URI, HttpMethod, ClientHttpResponse, HttpStatusCode)
156
+ */
157
+ @ Override
158
+ public void handleError (URI url , HttpMethod method , ClientHttpResponse response ) throws IOException {
136
159
HttpStatusCode statusCode = response .getStatusCode ();
137
- handleError (response , statusCode );
160
+ handleError (url , method , response , statusCode );
138
161
}
139
162
140
163
/**
@@ -144,10 +167,9 @@ public void handleError(ClientHttpResponse response) throws IOException {
144
167
* </pre>
145
168
*/
146
169
private String getErrorMessage (
147
- int rawStatusCode , String statusText , @ Nullable byte [] responseBody , @ Nullable Charset charset ) {
148
-
149
- String preface = rawStatusCode + " " + statusText + ": " ;
170
+ int rawStatusCode , String statusText , @ Nullable byte [] responseBody , @ Nullable Charset charset , @ Nullable URI url , @ Nullable HttpMethod method ) {
150
171
172
+ String preface = getPreface (rawStatusCode , statusText , url , method );
151
173
if (ObjectUtils .isEmpty (responseBody )) {
152
174
return preface + "[no body]" ;
153
175
}
@@ -160,6 +182,15 @@ private String getErrorMessage(
160
182
return preface + bodyText ;
161
183
}
162
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 (" " );
189
+ }
190
+ preface .append (": " );
191
+ return preface .toString ();
192
+ }
193
+
163
194
/**
164
195
* Handle the error based on the resolved status code.
165
196
*
@@ -172,11 +203,27 @@ private String getErrorMessage(
172
203
* @see HttpServerErrorException#create
173
204
*/
174
205
protected void handleError (ClientHttpResponse response , HttpStatusCode statusCode ) throws IOException {
206
+ handleError (null , null , response , statusCode );
207
+ }
208
+
209
+ /**
210
+ * Handle the error based on the resolved status code.
211
+ *
212
+ * <p>The default implementation delegates to
213
+ * {@link HttpClientErrorException#create} for errors in the 4xx range, to
214
+ * {@link HttpServerErrorException#create} for errors in the 5xx range,
215
+ * or otherwise raises {@link UnknownHttpStatusCodeException}.
216
+ * @since 5.0
217
+ * @see HttpClientErrorException#create
218
+ * @see HttpServerErrorException#create
219
+ */
220
+ protected void handleError (@ Nullable URI url , @ Nullable HttpMethod method , ClientHttpResponse response ,
221
+ HttpStatusCode statusCode ) throws IOException {
175
222
String statusText = response .getStatusText ();
176
223
HttpHeaders headers = response .getHeaders ();
177
224
byte [] body = getResponseBody (response );
178
225
Charset charset = getCharset (response );
179
- String message = getErrorMessage (statusCode .value (), statusText , body , charset );
226
+ String message = getErrorMessage (statusCode .value (), statusText , body , charset , url , method );
180
227
181
228
RestClientResponseException ex ;
182
229
if (statusCode .is4xxClientError ()) {
0 commit comments