DefaultResponseErrorHandler makes use of HttpStatus.isError()
Issue: SPR-17439
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -436,75 +436,82 @@ public enum HttpStatus {
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether this status code is in the HTTP series
|
||||
* {@link org.springframework.http.HttpStatus.Series#INFORMATIONAL}.
|
||||
* This is a shortcut for checking the value of {@link #series()}.
|
||||
*/
|
||||
public boolean is1xxInformational() {
|
||||
return Series.INFORMATIONAL.equals(series());
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether this status code is in the HTTP series
|
||||
* {@link org.springframework.http.HttpStatus.Series#SUCCESSFUL}.
|
||||
* This is a shortcut for checking the value of {@link #series()}.
|
||||
*/
|
||||
public boolean is2xxSuccessful() {
|
||||
return Series.SUCCESSFUL.equals(series());
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether this status code is in the HTTP series
|
||||
* {@link org.springframework.http.HttpStatus.Series#REDIRECTION}.
|
||||
* This is a shortcut for checking the value of {@link #series()}.
|
||||
*/
|
||||
public boolean is3xxRedirection() {
|
||||
return Series.REDIRECTION.equals(series());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Whether this status code is in the HTTP series
|
||||
* {@link org.springframework.http.HttpStatus.Series#CLIENT_ERROR}.
|
||||
* This is a shortcut for checking the value of {@link #series()}.
|
||||
*/
|
||||
public boolean is4xxClientError() {
|
||||
return Series.CLIENT_ERROR.equals(series());
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether this status code is in the HTTP series
|
||||
* {@link org.springframework.http.HttpStatus.Series#SERVER_ERROR}.
|
||||
* This is a shortcut for checking the value of {@link #series()}.
|
||||
*/
|
||||
public boolean is5xxServerError() {
|
||||
return Series.SERVER_ERROR.equals(series());
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether this status code is in the HTTP series
|
||||
* {@link org.springframework.http.HttpStatus.Series#CLIENT_ERROR} or
|
||||
* {@link org.springframework.http.HttpStatus.Series#SERVER_ERROR}.
|
||||
* This is a shortcut for checking the value of {@link #series()}.
|
||||
*/
|
||||
public boolean isError() {
|
||||
return is4xxClientError() || is5xxServerError();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the HTTP status series of this status code.
|
||||
* Return the HTTP status series of this status code.
|
||||
* @see HttpStatus.Series
|
||||
*/
|
||||
public Series series() {
|
||||
return Series.valueOf(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether this status code is in the HTTP series
|
||||
* {@link org.springframework.http.HttpStatus.Series#INFORMATIONAL}.
|
||||
* This is a shortcut for checking the value of {@link #series()}.
|
||||
* @see #series()
|
||||
*/
|
||||
public boolean is1xxInformational() {
|
||||
return (series() == Series.INFORMATIONAL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether this status code is in the HTTP series
|
||||
* {@link org.springframework.http.HttpStatus.Series#SUCCESSFUL}.
|
||||
* This is a shortcut for checking the value of {@link #series()}.
|
||||
* @see #series()
|
||||
*/
|
||||
public boolean is2xxSuccessful() {
|
||||
return (series() == Series.SUCCESSFUL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether this status code is in the HTTP series
|
||||
* {@link org.springframework.http.HttpStatus.Series#REDIRECTION}.
|
||||
* This is a shortcut for checking the value of {@link #series()}.
|
||||
* @see #series()
|
||||
*/
|
||||
public boolean is3xxRedirection() {
|
||||
return (series() == Series.REDIRECTION);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether this status code is in the HTTP series
|
||||
* {@link org.springframework.http.HttpStatus.Series#CLIENT_ERROR}.
|
||||
* This is a shortcut for checking the value of {@link #series()}.
|
||||
* @see #series()
|
||||
*/
|
||||
public boolean is4xxClientError() {
|
||||
return (series() == Series.CLIENT_ERROR);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether this status code is in the HTTP series
|
||||
* {@link org.springframework.http.HttpStatus.Series#SERVER_ERROR}.
|
||||
* This is a shortcut for checking the value of {@link #series()}.
|
||||
* @see #series()
|
||||
*/
|
||||
public boolean is5xxServerError() {
|
||||
return (series() == Series.SERVER_ERROR);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether this status code is in the HTTP series
|
||||
* {@link org.springframework.http.HttpStatus.Series#CLIENT_ERROR} or
|
||||
* {@link org.springframework.http.HttpStatus.Series#SERVER_ERROR}.
|
||||
* This is a shortcut for checking the value of {@link #series()}.
|
||||
* @since 5.0
|
||||
* @see #is4xxClientError()
|
||||
* @see #is5xxServerError()
|
||||
*/
|
||||
public boolean isError() {
|
||||
return (is4xxClientError() || is5xxServerError());
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a string representation of this status code.
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return Integer.toString(this.value) + " " + name();
|
||||
return this.value + " " + name();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -44,7 +44,11 @@ import org.springframework.util.FileCopyUtils;
|
||||
public class DefaultResponseErrorHandler implements ResponseErrorHandler {
|
||||
|
||||
/**
|
||||
* Delegates to {@link #hasError(HttpStatus)} with the response status code.
|
||||
* Delegates to {@link #hasError(HttpStatus)} (for a standard status enum value) or
|
||||
* {@link #hasError(int)} (for an unknown status code) with the response status code.
|
||||
* @see ClientHttpResponse#getRawStatusCode()
|
||||
* @see #hasError(HttpStatus)
|
||||
* @see #hasError(int)
|
||||
*/
|
||||
@Override
|
||||
public boolean hasError(ClientHttpResponse response) throws IOException {
|
||||
@@ -55,16 +59,14 @@ public class DefaultResponseErrorHandler implements ResponseErrorHandler {
|
||||
|
||||
/**
|
||||
* Template method called from {@link #hasError(ClientHttpResponse)}.
|
||||
* <p>The default implementation checks if the given status code is
|
||||
* {@code HttpStatus.Series#CLIENT_ERROR CLIENT_ERROR} or
|
||||
* {@code HttpStatus.Series#SERVER_ERROR SERVER_ERROR}.
|
||||
* <p>The default implementation checks {@link HttpStatus#isError()}.
|
||||
* Can be overridden in subclasses.
|
||||
* @param statusCode the HTTP status code as enum value
|
||||
* @return {@code true} if the response has an error; {@code false} otherwise
|
||||
* @return {@code true} if the response indicates an error; {@code false} otherwise
|
||||
* @see HttpStatus#isError()
|
||||
*/
|
||||
protected boolean hasError(HttpStatus statusCode) {
|
||||
return (statusCode.series() == HttpStatus.Series.CLIENT_ERROR ||
|
||||
statusCode.series() == HttpStatus.Series.SERVER_ERROR);
|
||||
return statusCode.isError();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -74,8 +76,10 @@ public class DefaultResponseErrorHandler implements ResponseErrorHandler {
|
||||
* {@code HttpStatus.Series#SERVER_ERROR SERVER_ERROR}.
|
||||
* Can be overridden in subclasses.
|
||||
* @param unknownStatusCode the HTTP status code as raw value
|
||||
* @return {@code true} if the response has an error; {@code false} otherwise
|
||||
* @return {@code true} if the response indicates an error; {@code false} otherwise
|
||||
* @since 4.3.21
|
||||
* @see HttpStatus.Series#CLIENT_ERROR
|
||||
* @see HttpStatus.Series#SERVER_ERROR
|
||||
*/
|
||||
protected boolean hasError(int unknownStatusCode) {
|
||||
HttpStatus.Series series = HttpStatus.Series.resolve(unknownStatusCode);
|
||||
@@ -83,7 +87,10 @@ public class DefaultResponseErrorHandler implements ResponseErrorHandler {
|
||||
}
|
||||
|
||||
/**
|
||||
* Delegates to {@link #handleError(ClientHttpResponse, HttpStatus)} with the response status code.
|
||||
* Delegates to {@link #handleError(ClientHttpResponse, HttpStatus)} with the
|
||||
* response status code.
|
||||
* @throws UnknownHttpStatusCodeException in case of an unresolvable status code
|
||||
* @see #handleError(ClientHttpResponse, HttpStatus)
|
||||
*/
|
||||
@Override
|
||||
public void handleError(ClientHttpResponse response) throws IOException {
|
||||
@@ -97,11 +104,13 @@ public class DefaultResponseErrorHandler implements ResponseErrorHandler {
|
||||
|
||||
/**
|
||||
* Handle the error in the given response with the given resolved status code.
|
||||
* <p>This default implementation throws a {@link HttpClientErrorException} if the response status code
|
||||
* is {@link org.springframework.http.HttpStatus.Series#CLIENT_ERROR}, a {@link HttpServerErrorException}
|
||||
* if it is {@link org.springframework.http.HttpStatus.Series#SERVER_ERROR},
|
||||
* and a {@link RestClientException} in other cases.
|
||||
* <p>The default implementation throws an {@link HttpClientErrorException}
|
||||
* if the status code is {@link HttpStatus.Series#CLIENT_ERROR}, an
|
||||
* {@link HttpServerErrorException} if it is {@link HttpStatus.Series#SERVER_ERROR},
|
||||
* and an {@link UnknownHttpStatusCodeException} in other cases.
|
||||
* @since 5.0
|
||||
* @see HttpClientErrorException#create
|
||||
* @see HttpServerErrorException#create
|
||||
*/
|
||||
protected void handleError(ClientHttpResponse response, HttpStatus statusCode) throws IOException {
|
||||
String statusText = response.getStatusText();
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -36,7 +36,7 @@ public interface ResponseErrorHandler {
|
||||
* <p>Implementations will typically inspect the
|
||||
* {@link ClientHttpResponse#getStatusCode() HttpStatus} of the response.
|
||||
* @param response the response to inspect
|
||||
* @return {@code true} if the response has an error; {@code false} otherwise
|
||||
* @return {@code true} if the response indicates an error; {@code false} otherwise
|
||||
* @throws IOException in case of I/O errors
|
||||
*/
|
||||
boolean hasError(ClientHttpResponse response) throws IOException;
|
||||
|
||||
Reference in New Issue
Block a user