Skip to content

Commit 898a031

Browse files
Merge pull request #281 from brendandburns/close
Close the response body in the case of error.
2 parents f10d8ba + c781984 commit 898a031

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

examples/src/main/java/io/kubernetes/client/examples/WatchExample.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,12 @@ public static void main(String[] args) throws IOException, ApiException {
3939
null, null, null, null, null, 5, null, null, Boolean.TRUE, null, null),
4040
new TypeToken<Watch.Response<V1Namespace>>() {}.getType());
4141

42-
for (Watch.Response<V1Namespace> item : watch) {
43-
System.out.printf("%s : %s%n", item.type, item.object.getMetadata().getName());
42+
try {
43+
for (Watch.Response<V1Namespace> item : watch) {
44+
System.out.printf("%s : %s%n", item.type, item.object.getMetadata().getName());
45+
}
46+
} finally {
47+
watch.close();
4448
}
4549
}
4650
}

util/src/main/java/io/kubernetes/client/util/Watch.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@
3434
* CoreV1Api.listNamespaceCall and set watch to True and watch the changes to namespaces.
3535
*/
3636
public class Watch<T>
37-
implements Iterable<Watch.Response<T>>, Iterator<Watch.Response<T>>, java.io.Closeable {
37+
implements Iterable<Watch.Response<T>>,
38+
Iterator<Watch.Response<T>>,
39+
java.io.Closeable,
40+
AutoCloseable {
3841

3942
private static final Logger log = LoggerFactory.getLogger(Watch.class);
4043

@@ -91,13 +94,13 @@ public static <T> Watch<T> createWatch(ApiClient client, Call call, Type watchTy
9194
com.squareup.okhttp.Response response = call.execute();
9295
if (!response.isSuccessful()) {
9396
String respBody = null;
94-
if (response.body() != null) {
95-
try {
97+
try (ResponseBody body = response.body()) {
98+
if (body != null) {
9699
respBody = response.body().string();
97-
} catch (IOException e) {
98-
throw new ApiException(
99-
response.message(), e, response.code(), response.headers().toMultimap());
100100
}
101+
} catch (IOException e) {
102+
throw new ApiException(
103+
response.message(), e, response.code(), response.headers().toMultimap());
101104
}
102105
throw new ApiException(
103106
response.message(), response.code(), response.headers().toMultimap(), respBody);

0 commit comments

Comments
 (0)