Skip to content

Commit a5cef38

Browse files
committed
Improve message for HttpStatusCodeException with empty status text
This commit improves the message for HttpStatusCodeException so that it defaults to the HttpStatus reason phrase if a status text is not provided. This commit also fixes SimpleClientHttpResponse so that it does not return null for getStatusText(). Fixed #22162
1 parent 155ef5f commit a5cef38

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

spring-web/src/main/java/org/springframework/http/client/SimpleClientHttpResponse.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -57,7 +57,8 @@ public int getRawStatusCode() throws IOException {
5757

5858
@Override
5959
public String getStatusText() throws IOException {
60-
return this.connection.getResponseMessage();
60+
String result = this.connection.getResponseMessage();
61+
return (result != null) ? result : "";
6162
}
6263

6364
@Override

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -21,6 +21,7 @@
2121
import org.springframework.http.HttpHeaders;
2222
import org.springframework.http.HttpStatus;
2323
import org.springframework.lang.Nullable;
24+
import org.springframework.util.StringUtils;
2425

2526
/**
2627
* Abstract base class for exceptions based on an {@link HttpStatus}.
@@ -82,11 +83,17 @@ protected HttpStatusCodeException(HttpStatus statusCode, String statusText,
8283
protected HttpStatusCodeException(HttpStatus statusCode, String statusText,
8384
@Nullable HttpHeaders responseHeaders, @Nullable byte[] responseBody, @Nullable Charset responseCharset) {
8485

85-
super(statusCode.value() + " " + statusText, statusCode.value(), statusText,
86+
super(getMessage(statusCode, statusText), statusCode.value(), statusText,
8687
responseHeaders, responseBody, responseCharset);
8788
this.statusCode = statusCode;
8889
}
8990

91+
private static String getMessage(HttpStatus statusCode, String statusText) {
92+
if (!StringUtils.hasLength(statusText)) {
93+
statusText = statusCode.getReasonPhrase();
94+
}
95+
return statusCode.value() + " " + statusText;
96+
}
9097

9198
/**
9299
* Return the HTTP status code.

spring-web/src/test/java/org/springframework/web/client/HttpStatusCodeExceptionTests.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -27,7 +27,7 @@
2727

2828
import org.springframework.http.HttpStatus;
2929

30-
import static org.hamcrest.CoreMatchers.*;
30+
import static org.hamcrest.CoreMatchers.equalTo;
3131
import static org.junit.Assert.*;
3232

3333
/**
@@ -54,4 +54,11 @@ public void testSerializability() throws IOException, ClassNotFoundException {
5454
assertThat(ex2.getResponseBodyAsString(), equalTo(ex1.getResponseBodyAsString()));
5555
}
5656

57+
@Test
58+
public void emptyStatusText() {
59+
HttpStatusCodeException ex = new HttpClientErrorException(HttpStatus.NOT_FOUND, "");
60+
61+
assertEquals("404 Not Found", ex.getMessage());
62+
}
63+
5764
}

0 commit comments

Comments
 (0)