Skip to content

Commit ada0741

Browse files
Polish Docker client exception handling
1 parent 6f095d6 commit ada0741

File tree

2 files changed

+10
-12
lines changed
  • spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src

2 files changed

+10
-12
lines changed

spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/HttpClientHttp.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
*
4949
* @author Phillip Webb
5050
* @author Mike Smithson
51+
* @author Scott Frederick
5152
*/
5253
class HttpClientHttp implements Http {
5354

@@ -129,9 +130,8 @@ private Response execute(HttpEntityEnclosingRequestBase request, String contentT
129130
}
130131

131132
private Response execute(HttpUriRequest request) {
132-
CloseableHttpResponse response;
133133
try {
134-
response = this.client.execute(request);
134+
CloseableHttpResponse response = this.client.execute(request);
135135
StatusLine statusLine = response.getStatusLine();
136136
int statusCode = statusLine.getStatusCode();
137137
HttpEntity entity = response.getEntity();
@@ -143,15 +143,11 @@ private Response execute(HttpUriRequest request) {
143143
if (statusCode == 500) {
144144
throw new DockerException(request.getURI(), statusCode, statusLine.getReasonPhrase(), null);
145145
}
146+
return new HttpClientResponse(response);
146147
}
147148
catch (IOException ioe) {
148-
StringWriter stringWriter = new StringWriter();
149-
PrintWriter printWriter = new PrintWriter(stringWriter);
150-
ioe.printStackTrace(printWriter);
151-
throw new DockerException(request.getURI(), 500, stringWriter.toString(), null);
149+
throw new DockerException(request.getURI(), 500, ioe.getMessage(), null);
152150
}
153-
154-
return new HttpClientResponse(response);
155151
}
156152

157153
/**

spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/docker/HttpClientHttpTests.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
*
5555
* @author Phillip Webb
5656
* @author Mike Smithson
57+
* @author Scott Frederick
5758
*/
5859
class HttpClientHttpTests {
5960

@@ -186,11 +187,12 @@ void executeWhenResposeIsIn500RangeShouldThrowDockerException() {
186187
}
187188

188189
@Test
189-
void executeWhenClientExecutesRequestThrowsIOExceptionRethrowsAsDockerException() throws IOException {
190-
given(this.client.execute(any())).willThrow(IOException.class);
190+
void executeWhenClientThrowsIOExceptionRethrowsAsDockerException() throws IOException {
191+
given(this.client.execute(any())).willThrow(new IOException("test IO exception"));
191192
assertThatExceptionOfType(DockerException.class).isThrownBy(() -> this.http.get(this.uri))
192-
.satisfies((ex) -> assertThat(ex.getErrors()).isNull()).satisfies(DockerException::getStatusCode)
193-
.withMessageContaining("500").satisfies((ex) -> assertThat(ex.getReasonPhrase())).isNotNull();
193+
.satisfies((ex) -> assertThat(ex.getErrors()).isNull())
194+
.satisfies(DockerException::getStatusCode).withMessageContaining("500")
195+
.satisfies((ex) -> assertThat(ex.getReasonPhrase()).contains("test IO exception"));
194196
}
195197

196198
private String writeToString(HttpEntity entity) throws IOException {

0 commit comments

Comments
 (0)