Skip to content

Commit 7f0e348

Browse files
jerzykrlkrstoyanchev
jerzykrlk
authored andcommitted
Fine-grained RestTemplate exception hierarchy
Issue: SPR-15404
1 parent 2054fa2 commit 7f0e348

File tree

6 files changed

+960
-3
lines changed

6 files changed

+960
-3
lines changed

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

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,77 @@ public void handleError(ClientHttpResponse response) throws IOException {
9090
protected void handleError(ClientHttpResponse response, HttpStatus statusCode) throws IOException {
9191
switch (statusCode.series()) {
9292
case CLIENT_ERROR:
93+
handleClientError(response, statusCode);
94+
return;
95+
case SERVER_ERROR:
96+
handleServerError(response, statusCode);
97+
return;
98+
default:
99+
throw new UnknownHttpStatusCodeException(statusCode.value(), response.getStatusText(),
100+
response.getHeaders(), getResponseBody(response), getCharset(response));
101+
}
102+
}
103+
104+
private void handleClientError(ClientHttpResponse response, HttpStatus statusCode) throws IOException {
105+
switch (statusCode) {
106+
case BAD_REQUEST:
107+
throw new HttpClientErrorException.BadRequest(response.getStatusText(),
108+
response.getHeaders(), getResponseBody(response), getCharset(response));
109+
case UNAUTHORIZED:
110+
throw new HttpClientErrorException.Unauthorized(response.getStatusText(),
111+
response.getHeaders(), getResponseBody(response), getCharset(response));
112+
case FORBIDDEN:
113+
throw new HttpClientErrorException.Forbidden(response.getStatusText(),
114+
response.getHeaders(), getResponseBody(response), getCharset(response));
115+
case NOT_FOUND:
116+
throw new HttpClientErrorException.NotFound(response.getStatusText(),
117+
response.getHeaders(), getResponseBody(response), getCharset(response));
118+
case METHOD_NOT_ALLOWED:
119+
throw new HttpClientErrorException.MethodNotAllowed(response.getStatusText(),
120+
response.getHeaders(), getResponseBody(response), getCharset(response));
121+
case NOT_ACCEPTABLE:
122+
throw new HttpClientErrorException.NotAcceptable(response.getStatusText(),
123+
response.getHeaders(), getResponseBody(response), getCharset(response));
124+
case CONFLICT:
125+
throw new HttpClientErrorException.Conflict(response.getStatusText(),
126+
response.getHeaders(), getResponseBody(response), getCharset(response));
127+
case GONE:
128+
throw new HttpClientErrorException.Gone(response.getStatusText(),
129+
response.getHeaders(), getResponseBody(response), getCharset(response));
130+
case UNSUPPORTED_MEDIA_TYPE:
131+
throw new HttpClientErrorException.UnsupportedMediaType(response.getStatusText(),
132+
response.getHeaders(), getResponseBody(response), getCharset(response));
133+
case TOO_MANY_REQUESTS:
134+
throw new HttpClientErrorException.TooManyRequests(response.getStatusText(),
135+
response.getHeaders(), getResponseBody(response), getCharset(response));
136+
case UNPROCESSABLE_ENTITY:
137+
throw new HttpClientErrorException.UnprocessableEntity(response.getStatusText(),
138+
response.getHeaders(), getResponseBody(response), getCharset(response));
139+
default:
93140
throw new HttpClientErrorException(statusCode, response.getStatusText(),
94141
response.getHeaders(), getResponseBody(response), getCharset(response));
95-
case SERVER_ERROR:
96-
throw new HttpServerErrorException(statusCode, response.getStatusText(),
142+
}
143+
}
144+
145+
private void handleServerError(ClientHttpResponse response, HttpStatus statusCode) throws IOException {
146+
switch (statusCode) {
147+
case INTERNAL_SERVER_ERROR:
148+
throw new HttpServerErrorException.InternalServerError(response.getStatusText(),
149+
response.getHeaders(), getResponseBody(response), getCharset(response));
150+
case NOT_IMPLEMENTED:
151+
throw new HttpServerErrorException.NotImplemented(response.getStatusText(),
152+
response.getHeaders(), getResponseBody(response), getCharset(response));
153+
case BAD_GATEWAY:
154+
throw new HttpServerErrorException.BadGateway(response.getStatusText(),
155+
response.getHeaders(), getResponseBody(response), getCharset(response));
156+
case SERVICE_UNAVAILABLE:
157+
throw new HttpServerErrorException.ServiceUnavailable(response.getStatusText(),
158+
response.getHeaders(), getResponseBody(response), getCharset(response));
159+
case GATEWAY_TIMEOUT:
160+
throw new HttpServerErrorException.GatewayTimeout(response.getStatusText(),
97161
response.getHeaders(), getResponseBody(response), getCharset(response));
98162
default:
99-
throw new UnknownHttpStatusCodeException(statusCode.value(), response.getStatusText(),
163+
throw new HttpServerErrorException(statusCode, response.getStatusText(),
100164
response.getHeaders(), getResponseBody(response), getCharset(response));
101165
}
102166
}

0 commit comments

Comments
 (0)