From a5cef3845ca4d7625e17a460441bd6f8656ca2f8 Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Wed, 23 Jan 2019 11:42:55 +0100 Subject: [PATCH] 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 --- .../http/client/SimpleClientHttpResponse.java | 5 +++-- .../web/client/HttpStatusCodeException.java | 11 +++++++++-- .../web/client/HttpStatusCodeExceptionTests.java | 11 +++++++++-- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/http/client/SimpleClientHttpResponse.java b/spring-web/src/main/java/org/springframework/http/client/SimpleClientHttpResponse.java index 22cf7b59c1..c70f38b358 100644 --- a/spring-web/src/main/java/org/springframework/http/client/SimpleClientHttpResponse.java +++ b/spring-web/src/main/java/org/springframework/http/client/SimpleClientHttpResponse.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -57,7 +57,8 @@ final class SimpleClientHttpResponse extends AbstractClientHttpResponse { @Override public String getStatusText() throws IOException { - return this.connection.getResponseMessage(); + String result = this.connection.getResponseMessage(); + return (result != null) ? result : ""; } @Override diff --git a/spring-web/src/main/java/org/springframework/web/client/HttpStatusCodeException.java b/spring-web/src/main/java/org/springframework/web/client/HttpStatusCodeException.java index a4f22478ed..2ca5f7195f 100644 --- a/spring-web/src/main/java/org/springframework/web/client/HttpStatusCodeException.java +++ b/spring-web/src/main/java/org/springframework/web/client/HttpStatusCodeException.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2019 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. @@ -21,6 +21,7 @@ import java.nio.charset.Charset; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.lang.Nullable; +import org.springframework.util.StringUtils; /** * Abstract base class for exceptions based on an {@link HttpStatus}. @@ -82,11 +83,17 @@ public abstract class HttpStatusCodeException extends RestClientResponseExceptio protected HttpStatusCodeException(HttpStatus statusCode, String statusText, @Nullable HttpHeaders responseHeaders, @Nullable byte[] responseBody, @Nullable Charset responseCharset) { - super(statusCode.value() + " " + statusText, statusCode.value(), statusText, + super(getMessage(statusCode, statusText), statusCode.value(), statusText, responseHeaders, responseBody, responseCharset); this.statusCode = statusCode; } + private static String getMessage(HttpStatus statusCode, String statusText) { + if (!StringUtils.hasLength(statusText)) { + statusText = statusCode.getReasonPhrase(); + } + return statusCode.value() + " " + statusText; + } /** * Return the HTTP status code. diff --git a/spring-web/src/test/java/org/springframework/web/client/HttpStatusCodeExceptionTests.java b/spring-web/src/test/java/org/springframework/web/client/HttpStatusCodeExceptionTests.java index 022772517d..605be4499d 100644 --- a/spring-web/src/test/java/org/springframework/web/client/HttpStatusCodeExceptionTests.java +++ b/spring-web/src/test/java/org/springframework/web/client/HttpStatusCodeExceptionTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2019 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. @@ -27,7 +27,7 @@ import org.junit.Test; import org.springframework.http.HttpStatus; -import static org.hamcrest.CoreMatchers.*; +import static org.hamcrest.CoreMatchers.equalTo; import static org.junit.Assert.*; /** @@ -54,4 +54,11 @@ public class HttpStatusCodeExceptionTests { assertThat(ex2.getResponseBodyAsString(), equalTo(ex1.getResponseBodyAsString())); } + @Test + public void emptyStatusText() { + HttpStatusCodeException ex = new HttpClientErrorException(HttpStatus.NOT_FOUND, ""); + + assertEquals("404 Not Found", ex.getMessage()); + } + }