Skip to content

Commit 97ac1c2

Browse files
committed
DefaultResponseErrorHandler makes use of HttpStatus.isError()
Issue: SPR-17439
1 parent a528407 commit 97ac1c2

File tree

3 files changed

+48
-32
lines changed

3 files changed

+48
-32
lines changed

spring-web/src/main/java/org/springframework/http/HttpStatus.java

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -435,76 +435,83 @@ public String getReasonPhrase() {
435435
return this.reasonPhrase;
436436
}
437437

438+
/**
439+
* Return the HTTP status series of this status code.
440+
* @see HttpStatus.Series
441+
*/
442+
public Series series() {
443+
return Series.valueOf(this);
444+
}
445+
438446
/**
439447
* Whether this status code is in the HTTP series
440448
* {@link org.springframework.http.HttpStatus.Series#INFORMATIONAL}.
441449
* This is a shortcut for checking the value of {@link #series()}.
450+
* @see #series()
442451
*/
443452
public boolean is1xxInformational() {
444-
return Series.INFORMATIONAL.equals(series());
453+
return (series() == Series.INFORMATIONAL);
445454
}
446455

447456
/**
448457
* Whether this status code is in the HTTP series
449458
* {@link org.springframework.http.HttpStatus.Series#SUCCESSFUL}.
450459
* This is a shortcut for checking the value of {@link #series()}.
460+
* @see #series()
451461
*/
452462
public boolean is2xxSuccessful() {
453-
return Series.SUCCESSFUL.equals(series());
463+
return (series() == Series.SUCCESSFUL);
454464
}
455465

456466
/**
457467
* Whether this status code is in the HTTP series
458468
* {@link org.springframework.http.HttpStatus.Series#REDIRECTION}.
459469
* This is a shortcut for checking the value of {@link #series()}.
470+
* @see #series()
460471
*/
461472
public boolean is3xxRedirection() {
462-
return Series.REDIRECTION.equals(series());
473+
return (series() == Series.REDIRECTION);
463474
}
464475

465-
466476
/**
467477
* Whether this status code is in the HTTP series
468478
* {@link org.springframework.http.HttpStatus.Series#CLIENT_ERROR}.
469479
* This is a shortcut for checking the value of {@link #series()}.
480+
* @see #series()
470481
*/
471482
public boolean is4xxClientError() {
472-
return Series.CLIENT_ERROR.equals(series());
483+
return (series() == Series.CLIENT_ERROR);
473484
}
474485

475486
/**
476487
* Whether this status code is in the HTTP series
477488
* {@link org.springframework.http.HttpStatus.Series#SERVER_ERROR}.
478489
* This is a shortcut for checking the value of {@link #series()}.
490+
* @see #series()
479491
*/
480492
public boolean is5xxServerError() {
481-
return Series.SERVER_ERROR.equals(series());
493+
return (series() == Series.SERVER_ERROR);
482494
}
483495

484496
/**
485497
* Whether this status code is in the HTTP series
486498
* {@link org.springframework.http.HttpStatus.Series#CLIENT_ERROR} or
487499
* {@link org.springframework.http.HttpStatus.Series#SERVER_ERROR}.
488500
* This is a shortcut for checking the value of {@link #series()}.
501+
* @since 5.0
502+
* @see #is4xxClientError()
503+
* @see #is5xxServerError()
489504
*/
490505
public boolean isError() {
491-
return is4xxClientError() || is5xxServerError();
492-
}
493-
494-
/**
495-
* Returns the HTTP status series of this status code.
496-
* @see HttpStatus.Series
497-
*/
498-
public Series series() {
499-
return Series.valueOf(this);
506+
return (is4xxClientError() || is5xxServerError());
500507
}
501508

502509
/**
503510
* Return a string representation of this status code.
504511
*/
505512
@Override
506513
public String toString() {
507-
return Integer.toString(this.value) + " " + name();
514+
return this.value + " " + name();
508515
}
509516

510517

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

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@
4444
public class DefaultResponseErrorHandler implements ResponseErrorHandler {
4545

4646
/**
47-
* Delegates to {@link #hasError(HttpStatus)} with the response status code.
47+
* Delegates to {@link #hasError(HttpStatus)} (for a standard status enum value) or
48+
* {@link #hasError(int)} (for an unknown status code) with the response status code.
49+
* @see ClientHttpResponse#getRawStatusCode()
50+
* @see #hasError(HttpStatus)
51+
* @see #hasError(int)
4852
*/
4953
@Override
5054
public boolean hasError(ClientHttpResponse response) throws IOException {
@@ -55,16 +59,14 @@ public boolean hasError(ClientHttpResponse response) throws IOException {
5559

5660
/**
5761
* Template method called from {@link #hasError(ClientHttpResponse)}.
58-
* <p>The default implementation checks if the given status code is
59-
* {@code HttpStatus.Series#CLIENT_ERROR CLIENT_ERROR} or
60-
* {@code HttpStatus.Series#SERVER_ERROR SERVER_ERROR}.
62+
* <p>The default implementation checks {@link HttpStatus#isError()}.
6163
* Can be overridden in subclasses.
6264
* @param statusCode the HTTP status code as enum value
63-
* @return {@code true} if the response has an error; {@code false} otherwise
65+
* @return {@code true} if the response indicates an error; {@code false} otherwise
66+
* @see HttpStatus#isError()
6467
*/
6568
protected boolean hasError(HttpStatus statusCode) {
66-
return (statusCode.series() == HttpStatus.Series.CLIENT_ERROR ||
67-
statusCode.series() == HttpStatus.Series.SERVER_ERROR);
69+
return statusCode.isError();
6870
}
6971

7072
/**
@@ -74,16 +76,21 @@ protected boolean hasError(HttpStatus statusCode) {
7476
* {@code HttpStatus.Series#SERVER_ERROR SERVER_ERROR}.
7577
* Can be overridden in subclasses.
7678
* @param unknownStatusCode the HTTP status code as raw value
77-
* @return {@code true} if the response has an error; {@code false} otherwise
79+
* @return {@code true} if the response indicates an error; {@code false} otherwise
7880
* @since 4.3.21
81+
* @see HttpStatus.Series#CLIENT_ERROR
82+
* @see HttpStatus.Series#SERVER_ERROR
7983
*/
8084
protected boolean hasError(int unknownStatusCode) {
8185
HttpStatus.Series series = HttpStatus.Series.resolve(unknownStatusCode);
8286
return (series == HttpStatus.Series.CLIENT_ERROR || series == HttpStatus.Series.SERVER_ERROR);
8387
}
8488

8589
/**
86-
* Delegates to {@link #handleError(ClientHttpResponse, HttpStatus)} with the response status code.
90+
* Delegates to {@link #handleError(ClientHttpResponse, HttpStatus)} with the
91+
* response status code.
92+
* @throws UnknownHttpStatusCodeException in case of an unresolvable status code
93+
* @see #handleError(ClientHttpResponse, HttpStatus)
8794
*/
8895
@Override
8996
public void handleError(ClientHttpResponse response) throws IOException {
@@ -97,11 +104,13 @@ public void handleError(ClientHttpResponse response) throws IOException {
97104

98105
/**
99106
* Handle the error in the given response with the given resolved status code.
100-
* <p>This default implementation throws a {@link HttpClientErrorException} if the response status code
101-
* is {@link org.springframework.http.HttpStatus.Series#CLIENT_ERROR}, a {@link HttpServerErrorException}
102-
* if it is {@link org.springframework.http.HttpStatus.Series#SERVER_ERROR},
103-
* and a {@link RestClientException} in other cases.
107+
* <p>The default implementation throws an {@link HttpClientErrorException}
108+
* if the status code is {@link HttpStatus.Series#CLIENT_ERROR}, an
109+
* {@link HttpServerErrorException} if it is {@link HttpStatus.Series#SERVER_ERROR},
110+
* and an {@link UnknownHttpStatusCodeException} in other cases.
104111
* @since 5.0
112+
* @see HttpClientErrorException#create
113+
* @see HttpServerErrorException#create
105114
*/
106115
protected void handleError(ClientHttpResponse response, HttpStatus statusCode) throws IOException {
107116
String statusText = response.getStatusText();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,7 +36,7 @@ public interface ResponseErrorHandler {
3636
* <p>Implementations will typically inspect the
3737
* {@link ClientHttpResponse#getStatusCode() HttpStatus} of the response.
3838
* @param response the response to inspect
39-
* @return {@code true} if the response has an error; {@code false} otherwise
39+
* @return {@code true} if the response indicates an error; {@code false} otherwise
4040
* @throws IOException in case of I/O errors
4141
*/
4242
boolean hasError(ClientHttpResponse response) throws IOException;

0 commit comments

Comments
 (0)