Improve error message from image building

Translate IOException to DockerException for a more meaningful error
message when the Docker daemon is not available.

Fixes gh-20151
This commit is contained in:
Mike Smithson
2020-02-21 18:29:18 -05:00
committed by Scott Frederick
parent e73ee7b3fe
commit 6f095d6fec
2 changed files with 44 additions and 33 deletions

View File

@@ -26,7 +26,6 @@ import org.apache.http.HttpEntity;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpHeaders;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
@@ -54,6 +53,7 @@ import static org.mockito.Mockito.verify;
* Tests for {@link HttpClientHttp}.
*
* @author Phillip Webb
* @author Mike Smithson
*/
class HttpClientHttpTests {
@@ -132,7 +132,7 @@ class HttpClientHttpTests {
assertThat(entity.isRepeatable()).isFalse();
assertThat(entity.getContentLength()).isEqualTo(-1);
assertThat(entity.isStreaming()).isTrue();
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> entity.getContent());
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(entity::getContent);
assertThat(writeToString(entity)).isEqualTo("test");
assertThat(response.getContent()).isSameAs(this.content);
}
@@ -152,7 +152,7 @@ class HttpClientHttpTests {
assertThat(entity.isRepeatable()).isFalse();
assertThat(entity.getContentLength()).isEqualTo(-1);
assertThat(entity.isStreaming()).isTrue();
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() -> entity.getContent());
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(entity::getContent);
assertThat(writeToString(entity)).isEqualTo("test");
assertThat(response.getContent()).isSameAs(this.content);
}
@@ -171,7 +171,7 @@ class HttpClientHttpTests {
}
@Test
void executeWhenResposeIsIn400RangeShouldThrowDockerException() throws ClientProtocolException, IOException {
void executeWhenResposeIsIn400RangeShouldThrowDockerException() throws IOException {
given(this.entity.getContent()).willReturn(getClass().getResourceAsStream("errors.json"));
given(this.statusLine.getStatusCode()).willReturn(404);
assertThatExceptionOfType(DockerException.class).isThrownBy(() -> this.http.get(this.uri))
@@ -179,12 +179,20 @@ class HttpClientHttpTests {
}
@Test
void executeWhenResposeIsIn500RangeShouldThrowDockerException() throws ClientProtocolException, IOException {
void executeWhenResposeIsIn500RangeShouldThrowDockerException() {
given(this.statusLine.getStatusCode()).willReturn(500);
assertThatExceptionOfType(DockerException.class).isThrownBy(() -> this.http.get(this.uri))
.satisfies((ex) -> assertThat(ex.getErrors()).isNull());
}
@Test
void executeWhenClientExecutesRequestThrowsIOExceptionRethrowsAsDockerException() throws IOException {
given(this.client.execute(any())).willThrow(IOException.class);
assertThatExceptionOfType(DockerException.class).isThrownBy(() -> this.http.get(this.uri))
.satisfies((ex) -> assertThat(ex.getErrors()).isNull()).satisfies(DockerException::getStatusCode)
.withMessageContaining("500").satisfies((ex) -> assertThat(ex.getReasonPhrase())).isNotNull();
}
private String writeToString(HttpEntity entity) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
entity.writeTo(out);