Merge pull request #129 from apache/improved-httpexception-message

Improve HttpResponseException#getMessage()
This commit is contained in:
Oleg Kalnichevski 2018-12-23 23:09:38 +01:00 committed by GitHub
commit bf18e7d04e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 4 deletions

View File

@ -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;
}
}

View File

@ -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();