Skip to content

Commit 2216964

Browse files
committed
Polish RestTemplate exception hierarchy
Issue: SPR-15404
1 parent 7f0e348 commit 2216964

File tree

4 files changed

+155
-778
lines changed

4 files changed

+155
-778
lines changed

spring-web/src/main/java/org/springframework/web/client/DefaultResponseErrorHandler.java

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -88,84 +88,74 @@ public void handleError(ClientHttpResponse response) throws IOException {
8888
* @since 5.0
8989
*/
9090
protected void handleError(ClientHttpResponse response, HttpStatus statusCode) throws IOException {
91+
92+
String statusText = response.getStatusText();
93+
HttpHeaders headers = response.getHeaders();
94+
byte[] body = getResponseBody(response);
95+
Charset charset = getCharset(response);
96+
9197
switch (statusCode.series()) {
9298
case CLIENT_ERROR:
93-
handleClientError(response, statusCode);
99+
handleClientError(statusCode, statusText, headers, body, charset);
94100
return;
95101
case SERVER_ERROR:
96-
handleServerError(response, statusCode);
102+
handleServerError(statusCode, statusText, headers, body, charset);
97103
return;
98104
default:
99-
throw new UnknownHttpStatusCodeException(statusCode.value(), response.getStatusText(),
100-
response.getHeaders(), getResponseBody(response), getCharset(response));
105+
throw new UnknownHttpStatusCodeException(statusCode.value(), statusText, headers, body, charset);
101106
}
102107
}
103108

104-
private void handleClientError(ClientHttpResponse response, HttpStatus statusCode) throws IOException {
109+
private void handleClientError(HttpStatus statusCode,
110+
String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) {
111+
105112
switch (statusCode) {
106113
case BAD_REQUEST:
107-
throw new HttpClientErrorException.BadRequest(response.getStatusText(),
108-
response.getHeaders(), getResponseBody(response), getCharset(response));
114+
throw new HttpClientErrorException.BadRequest(statusText, headers, body, charset);
109115
case UNAUTHORIZED:
110-
throw new HttpClientErrorException.Unauthorized(response.getStatusText(),
111-
response.getHeaders(), getResponseBody(response), getCharset(response));
116+
throw new HttpClientErrorException.Unauthorized(statusText, headers, body, charset);
112117
case FORBIDDEN:
113-
throw new HttpClientErrorException.Forbidden(response.getStatusText(),
114-
response.getHeaders(), getResponseBody(response), getCharset(response));
118+
throw new HttpClientErrorException.Forbidden(statusText, headers, body, charset);
115119
case NOT_FOUND:
116-
throw new HttpClientErrorException.NotFound(response.getStatusText(),
117-
response.getHeaders(), getResponseBody(response), getCharset(response));
120+
throw new HttpClientErrorException.NotFound(statusText, headers, body, charset);
118121
case METHOD_NOT_ALLOWED:
119-
throw new HttpClientErrorException.MethodNotAllowed(response.getStatusText(),
120-
response.getHeaders(), getResponseBody(response), getCharset(response));
122+
throw new HttpClientErrorException.MethodNotAllowed(statusText, headers, body, charset);
121123
case NOT_ACCEPTABLE:
122-
throw new HttpClientErrorException.NotAcceptable(response.getStatusText(),
123-
response.getHeaders(), getResponseBody(response), getCharset(response));
124+
throw new HttpClientErrorException.NotAcceptable(statusText, headers, body, charset);
124125
case CONFLICT:
125-
throw new HttpClientErrorException.Conflict(response.getStatusText(),
126-
response.getHeaders(), getResponseBody(response), getCharset(response));
126+
throw new HttpClientErrorException.Conflict(statusText, headers, body, charset);
127127
case GONE:
128-
throw new HttpClientErrorException.Gone(response.getStatusText(),
129-
response.getHeaders(), getResponseBody(response), getCharset(response));
128+
throw new HttpClientErrorException.Gone(statusText, headers, body, charset);
130129
case UNSUPPORTED_MEDIA_TYPE:
131-
throw new HttpClientErrorException.UnsupportedMediaType(response.getStatusText(),
132-
response.getHeaders(), getResponseBody(response), getCharset(response));
130+
throw new HttpClientErrorException.UnsupportedMediaType(statusText, headers, body, charset);
133131
case TOO_MANY_REQUESTS:
134-
throw new HttpClientErrorException.TooManyRequests(response.getStatusText(),
135-
response.getHeaders(), getResponseBody(response), getCharset(response));
132+
throw new HttpClientErrorException.TooManyRequests(statusText, headers, body, charset);
136133
case UNPROCESSABLE_ENTITY:
137-
throw new HttpClientErrorException.UnprocessableEntity(response.getStatusText(),
138-
response.getHeaders(), getResponseBody(response), getCharset(response));
134+
throw new HttpClientErrorException.UnprocessableEntity(statusText, headers, body, charset);
139135
default:
140-
throw new HttpClientErrorException(statusCode, response.getStatusText(),
141-
response.getHeaders(), getResponseBody(response), getCharset(response));
136+
throw new HttpClientErrorException(statusCode, statusText, headers, body, charset);
142137
}
143138
}
144139

145-
private void handleServerError(ClientHttpResponse response, HttpStatus statusCode) throws IOException {
140+
private void handleServerError(HttpStatus statusCode,
141+
String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) {
142+
146143
switch (statusCode) {
147144
case INTERNAL_SERVER_ERROR:
148-
throw new HttpServerErrorException.InternalServerError(response.getStatusText(),
149-
response.getHeaders(), getResponseBody(response), getCharset(response));
145+
throw new HttpServerErrorException.InternalServerError(statusText, headers, body, charset);
150146
case NOT_IMPLEMENTED:
151-
throw new HttpServerErrorException.NotImplemented(response.getStatusText(),
152-
response.getHeaders(), getResponseBody(response), getCharset(response));
147+
throw new HttpServerErrorException.NotImplemented(statusText, headers, body, charset);
153148
case BAD_GATEWAY:
154-
throw new HttpServerErrorException.BadGateway(response.getStatusText(),
155-
response.getHeaders(), getResponseBody(response), getCharset(response));
149+
throw new HttpServerErrorException.BadGateway(statusText, headers, body, charset);
156150
case SERVICE_UNAVAILABLE:
157-
throw new HttpServerErrorException.ServiceUnavailable(response.getStatusText(),
158-
response.getHeaders(), getResponseBody(response), getCharset(response));
151+
throw new HttpServerErrorException.ServiceUnavailable(statusText, headers, body, charset);
159152
case GATEWAY_TIMEOUT:
160-
throw new HttpServerErrorException.GatewayTimeout(response.getStatusText(),
161-
response.getHeaders(), getResponseBody(response), getCharset(response));
153+
throw new HttpServerErrorException.GatewayTimeout(statusText, headers, body, charset);
162154
default:
163-
throw new HttpServerErrorException(statusCode, response.getStatusText(),
164-
response.getHeaders(), getResponseBody(response), getCharset(response));
155+
throw new HttpServerErrorException(statusCode, statusText, headers, body, charset);
165156
}
166157
}
167158

168-
169159
/**
170160
* Determine the HTTP status of the given response.
171161
* @param response the response to inspect

0 commit comments

Comments
 (0)