17
17
package org .springframework .web .client ;
18
18
19
19
import java .io .IOException ;
20
+ import java .net .URI ;
20
21
import java .nio .charset .Charset ;
21
22
22
23
import org .springframework .http .HttpHeaders ;
24
+ import org .springframework .http .HttpMethod ;
23
25
import org .springframework .http .HttpStatus ;
24
26
import org .springframework .http .MediaType ;
25
27
import org .springframework .http .client .ClientHttpResponse ;
26
28
import org .springframework .lang .Nullable ;
29
+ import org .springframework .util .Assert ;
27
30
import org .springframework .util .FileCopyUtils ;
28
31
29
32
/**
43
46
*/
44
47
public class DefaultResponseErrorHandler implements ResponseErrorHandler {
45
48
49
+ private HttpErrorDetailsExtractor httpErrorDetailsExtractor = new DefaultHttpErrorDetailsExtractor ();
50
+
51
+ /**
52
+ * Set the error summary extractor.
53
+ * <p>By default, DefaultResponseErrorHandler uses a {@link DefaultHttpErrorDetailsExtractor}.
54
+ */
55
+ public void setHttpErrorDetailsExtractor (HttpErrorDetailsExtractor httpErrorDetailsExtractor ) {
56
+ Assert .notNull (httpErrorDetailsExtractor , "HttpErrorDetailsExtractor must not be null" );
57
+ this .httpErrorDetailsExtractor = httpErrorDetailsExtractor ;
58
+ }
59
+
46
60
/**
47
61
* Delegates to {@link #hasError(HttpStatus)} (for a standard status enum value) or
48
62
* {@link #hasError(int)} (for an unknown status code) with the response status code.
@@ -87,19 +101,31 @@ protected boolean hasError(int unknownStatusCode) {
87
101
}
88
102
89
103
/**
90
- * Delegates to {@link #handleError(ClientHttpResponse, HttpStatus)} with the
104
+ * Delegates to {@link #handleError(URI, HttpMethod, ClientHttpResponse, HttpStatus)} with the
91
105
* response status code.
92
106
* @throws UnknownHttpStatusCodeException in case of an unresolvable status code
93
- * @see #handleError(ClientHttpResponse, HttpStatus)
107
+ * @see #handleError(URI, HttpMethod, ClientHttpResponse, HttpStatus)
94
108
*/
95
109
@ Override
96
110
public void handleError (ClientHttpResponse response ) throws IOException {
111
+ handleError (null , null , response );
112
+ }
113
+
114
+ /**
115
+ * Delegates to {@link #handleError(URI, HttpMethod, ClientHttpResponse, HttpStatus)} with the
116
+ * response status code.
117
+ * @throws UnknownHttpStatusCodeException in case of an unresolvable status code
118
+ * @see #handleError(URI, HttpMethod, ClientHttpResponse, HttpStatus)
119
+ */
120
+ @ Override
121
+ public void handleError (URI url , HttpMethod method , ClientHttpResponse response ) throws IOException {
97
122
HttpStatus statusCode = HttpStatus .resolve (response .getRawStatusCode ());
98
123
if (statusCode == null ) {
99
- throw new UnknownHttpStatusCodeException (response .getRawStatusCode (), response .getStatusText (),
100
- response .getHeaders (), getResponseBody (response ), getCharset (response ));
124
+ String message = httpErrorDetailsExtractor .getErrorDetails (response .getRawStatusCode (), response .getStatusText (), getResponseBody (response ), getCharset (response ), url , method );
125
+ throw new UnknownHttpStatusCodeException (message , response .getRawStatusCode (), response .getStatusText (),
126
+ response .getHeaders (), getResponseBody (response ), getCharset (response ), url , method );
101
127
}
102
- handleError (response , statusCode );
128
+ handleError (url , method , response , statusCode );
103
129
}
104
130
105
131
/**
@@ -114,17 +140,34 @@ public void handleError(ClientHttpResponse response) throws IOException {
114
140
* @see HttpServerErrorException#create
115
141
*/
116
142
protected void handleError (ClientHttpResponse response , HttpStatus statusCode ) throws IOException {
143
+ handleError (null , null , response , statusCode );
144
+ }
145
+
146
+ /**
147
+ * Handle the error in the given response with the given resolved status code.
148
+ * <p>This default implementation throws a {@link HttpClientErrorException} if the response status code
149
+ * is {@link org.springframework.http.HttpStatus.Series#CLIENT_ERROR}, a {@link HttpServerErrorException}
150
+ * if it is {@link org.springframework.http.HttpStatus.Series#SERVER_ERROR},
151
+ * and a {@link RestClientException} in other cases.
152
+ * @since 5.0
153
+ */
154
+ protected void handleError (@ Nullable URI url , @ Nullable HttpMethod method , ClientHttpResponse response ,
155
+ HttpStatus statusCode ) throws IOException {
156
+
117
157
String statusText = response .getStatusText ();
118
158
HttpHeaders headers = response .getHeaders ();
119
159
byte [] body = getResponseBody (response );
120
160
Charset charset = getCharset (response );
161
+ String message = httpErrorDetailsExtractor .getErrorDetails (statusCode .value (), statusText , body , charset , url , method );
162
+
121
163
switch (statusCode .series ()) {
122
164
case CLIENT_ERROR :
123
- throw HttpClientErrorException .create (statusCode , statusText , headers , body , charset );
165
+ throw HttpClientErrorException .create (message , statusCode , statusText , headers , body , charset , url , method );
124
166
case SERVER_ERROR :
125
- throw HttpServerErrorException .create (statusCode , statusText , headers , body , charset );
167
+ throw HttpServerErrorException .create (message , statusCode , statusText , headers , body , charset , url , method );
126
168
default :
127
- throw new UnknownHttpStatusCodeException (statusCode .value (), statusText , headers , body , charset );
169
+ throw new UnknownHttpStatusCodeException (message , statusCode .value (), statusText , headers , body ,
170
+ charset , url , method );
128
171
}
129
172
}
130
173
0 commit comments