Polish Http[Client|Server]ErrorException hierarchy

This commit is contained in:
Rossen Stoyanchev
2018-08-08 13:34:27 +03:00
parent 52f6dcf525
commit c90ab5fb0a
3 changed files with 62 additions and 55 deletions

View File

@@ -92,69 +92,16 @@ public class DefaultResponseErrorHandler implements ResponseErrorHandler {
HttpHeaders headers = response.getHeaders();
byte[] body = getResponseBody(response);
Charset charset = getCharset(response);
switch (statusCode.series()) {
case CLIENT_ERROR:
handleClientError(statusCode, statusText, headers, body, charset);
return;
throw HttpClientErrorException.create(statusCode, statusText, headers, body, charset);
case SERVER_ERROR:
handleServerError(statusCode, statusText, headers, body, charset);
return;
throw HttpServerErrorException.create(statusCode, statusText, headers, body, charset);
default:
throw new UnknownHttpStatusCodeException(statusCode.value(), statusText, headers, body, charset);
}
}
private void handleClientError(
HttpStatus statusCode, String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) {
switch (statusCode) {
case BAD_REQUEST:
throw new HttpClientErrorException.BadRequest(statusText, headers, body, charset);
case UNAUTHORIZED:
throw new HttpClientErrorException.Unauthorized(statusText, headers, body, charset);
case FORBIDDEN:
throw new HttpClientErrorException.Forbidden(statusText, headers, body, charset);
case NOT_FOUND:
throw new HttpClientErrorException.NotFound(statusText, headers, body, charset);
case METHOD_NOT_ALLOWED:
throw new HttpClientErrorException.MethodNotAllowed(statusText, headers, body, charset);
case NOT_ACCEPTABLE:
throw new HttpClientErrorException.NotAcceptable(statusText, headers, body, charset);
case CONFLICT:
throw new HttpClientErrorException.Conflict(statusText, headers, body, charset);
case GONE:
throw new HttpClientErrorException.Gone(statusText, headers, body, charset);
case UNSUPPORTED_MEDIA_TYPE:
throw new HttpClientErrorException.UnsupportedMediaType(statusText, headers, body, charset);
case TOO_MANY_REQUESTS:
throw new HttpClientErrorException.TooManyRequests(statusText, headers, body, charset);
case UNPROCESSABLE_ENTITY:
throw new HttpClientErrorException.UnprocessableEntity(statusText, headers, body, charset);
default:
throw new HttpClientErrorException(statusCode, statusText, headers, body, charset);
}
}
private void handleServerError(
HttpStatus statusCode, String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) {
switch (statusCode) {
case INTERNAL_SERVER_ERROR:
throw new HttpServerErrorException.InternalServerError(statusText, headers, body, charset);
case NOT_IMPLEMENTED:
throw new HttpServerErrorException.NotImplemented(statusText, headers, body, charset);
case BAD_GATEWAY:
throw new HttpServerErrorException.BadGateway(statusText, headers, body, charset);
case SERVICE_UNAVAILABLE:
throw new HttpServerErrorException.ServiceUnavailable(statusText, headers, body, charset);
case GATEWAY_TIMEOUT:
throw new HttpServerErrorException.GatewayTimeout(statusText, headers, body, charset);
default:
throw new HttpServerErrorException(statusCode, statusText, headers, body, charset);
}
}
/**
* Determine the HTTP status of the given response.
* @param response the response to inspect

View File

@@ -67,6 +67,42 @@ public class HttpClientErrorException extends HttpStatusCodeException {
}
/**
* Create {@code HttpClientErrorException} or an HTTP status specific sub-class.
* @since 5.1
*/
public static HttpClientErrorException create(
HttpStatus statusCode, String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) {
switch (statusCode) {
case BAD_REQUEST:
return new HttpClientErrorException.BadRequest(statusText, headers, body, charset);
case UNAUTHORIZED:
return new HttpClientErrorException.Unauthorized(statusText, headers, body, charset);
case FORBIDDEN:
return new HttpClientErrorException.Forbidden(statusText, headers, body, charset);
case NOT_FOUND:
return new HttpClientErrorException.NotFound(statusText, headers, body, charset);
case METHOD_NOT_ALLOWED:
return new HttpClientErrorException.MethodNotAllowed(statusText, headers, body, charset);
case NOT_ACCEPTABLE:
return new HttpClientErrorException.NotAcceptable(statusText, headers, body, charset);
case CONFLICT:
return new HttpClientErrorException.Conflict(statusText, headers, body, charset);
case GONE:
return new HttpClientErrorException.Gone(statusText, headers, body, charset);
case UNSUPPORTED_MEDIA_TYPE:
return new HttpClientErrorException.UnsupportedMediaType(statusText, headers, body, charset);
case TOO_MANY_REQUESTS:
return new HttpClientErrorException.TooManyRequests(statusText, headers, body, charset);
case UNPROCESSABLE_ENTITY:
return new HttpClientErrorException.UnprocessableEntity(statusText, headers, body, charset);
default:
return new HttpClientErrorException(statusCode, statusText, headers, body, charset);
}
}
// Subclasses for specific HTTP status codes
/**

View File

@@ -67,6 +67,30 @@ public class HttpServerErrorException extends HttpStatusCodeException {
}
/**
* Create an {@code HttpServerErrorException} or an HTTP status specific sub-class.
* @since 5.1
*/
public static HttpServerErrorException create(
HttpStatus statusCode, String statusText, HttpHeaders headers, byte[] body, @Nullable Charset charset) {
switch (statusCode) {
case INTERNAL_SERVER_ERROR:
return new HttpServerErrorException.InternalServerError(statusText, headers, body, charset);
case NOT_IMPLEMENTED:
return new HttpServerErrorException.NotImplemented(statusText, headers, body, charset);
case BAD_GATEWAY:
return new HttpServerErrorException.BadGateway(statusText, headers, body, charset);
case SERVICE_UNAVAILABLE:
return new HttpServerErrorException.ServiceUnavailable(statusText, headers, body, charset);
case GATEWAY_TIMEOUT:
return new HttpServerErrorException.GatewayTimeout(statusText, headers, body, charset);
default:
return new HttpServerErrorException(statusCode, statusText, headers, body, charset);
}
}
// Subclasses for specific HTTP status codes
/**