You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by mi...@apache.org on 2018/12/23 20:37:52 UTC

[httpcomponents-client] 01/01: Improve HttpResponseException#getMessage()

This is an automated email from the ASF dual-hosted git repository.

michaelo pushed a commit to branch improved-httpexception-message
in repository https://gitbox.apache.org/repos/asf/httpcomponents-client.git

commit 4450221be7bb56737be25ee5236500be50e03964
Author: Michael Osipov <mi...@apache.org>
AuthorDate: Sun Dec 23 21:18:13 2018 +0100

    Improve HttpResponseException#getMessage()
    
    The #getMessage() now properly consists of the status code as such and the
    optional reason phrase. Moreover, the pure reason phrase can be retrieved
    via #getReasonPhrase.
---
 .../org/apache/hc/client5/http/HttpResponseException.java     | 11 +++++++++--
 .../impl/classic/TestAbstractHttpClientResponseHandler.java   |  6 ++++--
 2 files changed, 13 insertions(+), 4 deletions(-)

diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/HttpResponseException.java b/httpclient5/src/main/java/org/apache/hc/client5/http/HttpResponseException.java
index fab64d7..228c7d6 100644
--- a/httpclient5/src/main/java/org/apache/hc/client5/http/HttpResponseException.java
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/HttpResponseException.java
@@ -38,14 +38,21 @@ public class HttpResponseException extends ClientProtocolException {
     private static final long serialVersionUID = -7186627969477257933L;
 
     private final int statusCode;
+    private final String reasonPhrase;
 
-    public HttpResponseException(final int statusCode, final String s) {
-        super(TextUtils.isBlank(s) ? Integer.toString(statusCode) : s);
+    public HttpResponseException(final int statusCode, final String reasonPhrase) {
+        super(String.format("status code: %d" +
+                (TextUtils.isBlank(reasonPhrase) ? "" : ", reason phrase: %s"), statusCode, reasonPhrase));
         this.statusCode = statusCode;
+        this.reasonPhrase = reasonPhrase;
     }
 
     public int getStatusCode() {
         return this.statusCode;
     }
 
+    public String getReasonPhrase() {
+        return this.reasonPhrase;
+    }
+
 }
diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestAbstractHttpClientResponseHandler.java b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestAbstractHttpClientResponseHandler.java
index d3e9c53..769d218 100644
--- a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestAbstractHttpClientResponseHandler.java
+++ b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestAbstractHttpClientResponseHandler.java
@@ -80,7 +80,8 @@ public class TestAbstractHttpClientResponseHandler {
             Assert.fail("HttpResponseException expected");
         } catch (final HttpResponseException ex) {
             Assert.assertEquals(404, ex.getStatusCode());
-            Assert.assertEquals("NOT FOUND", ex.getMessage());
+            Assert.assertEquals("NOT FOUND", ex.getReasonPhrase());
+            Assert.assertEquals("status code: 404, reason phrase: NOT FOUND", ex.getMessage());
         }
         Mockito.verify(entity).getContent();
         Mockito.verify(inStream).close();
@@ -103,7 +104,8 @@ public class TestAbstractHttpClientResponseHandler {
             Assert.fail("HttpResponseException expected");
         } catch (final HttpResponseException ex) {
             Assert.assertEquals(404, ex.getStatusCode());
-            Assert.assertEquals("404", ex.getMessage());
+            Assert.assertNull(ex.getReasonPhrase());
+            Assert.assertEquals("status code: 404", ex.getMessage());
         }
         Mockito.verify(entity).getContent();
         Mockito.verify(inStream).close();