提交 97ac1c22 编写于 作者: J Juergen Hoeller

DefaultResponseErrorHandler makes use of HttpStatus.isError()

Issue: SPR-17439
上级 a528407d
/*
* 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.
......@@ -435,50 +435,62 @@ public enum HttpStatus {
return this.reasonPhrase;
}
/**
* 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.INFORMATIONAL.equals(series());
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.SUCCESSFUL.equals(series());
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.REDIRECTION.equals(series());
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.CLIENT_ERROR.equals(series());
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.SERVER_ERROR.equals(series());
return (series() == Series.SERVER_ERROR);
}
/**
......@@ -486,17 +498,12 @@ public enum HttpStatus {
* {@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();
}
/**
* Returns the HTTP status series of this status code.
* @see HttpStatus.Series
*/
public Series series() {
return Series.valueOf(this);
return (is4xxClientError() || is5xxServerError());
}
/**
......@@ -504,7 +511,7 @@ public enum HttpStatus {
*/
@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();
......
/*
* 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;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册