Skip to content

Commit d854b2f

Browse files
committed
Refined exception handling in CompositeKeyMaterialFactory while I am here
1 parent 2171c32 commit d854b2f

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

src/main/java/org/jenkinsci/plugins/docker/commons/impl/CompositeKeyMaterialFactory.java

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,16 @@ public KeyMaterial2 materialize2() throws IOException, InterruptedException {
7979
if (keyMaterials[index] != null) {
8080
keyMaterials[index].close(getChannel());
8181
}
82-
} catch (IOException ioe) {
83-
// ignore as we want to try and close them all and we are reporting the original exception
8482
} catch (Throwable t) {
85-
// ignore as we want to try and close them all and we are reporting the original exception
83+
e.addSuppressed(t);
8684
}
8785
}
88-
// TODO Java 7+ use chained exceptions
89-
if (e instanceof IOException) {
90-
throw (IOException) e;
91-
} else if (e instanceof InterruptedException) {
92-
throw (InterruptedException) e;
93-
} else if (e instanceof RuntimeException) {
94-
throw (RuntimeException) e;
86+
if (e instanceof IOException ioe) {
87+
throw ioe;
88+
} else if (e instanceof InterruptedException ie) {
89+
throw ie;
90+
} else if (e instanceof RuntimeException re) {
91+
throw re;
9592
} else {
9693
throw new IOException("Error materializing credentials.", e);
9794
}
@@ -110,22 +107,28 @@ private static final class CompositeKeyMaterial2 extends KeyMaterial2 implements
110107
}
111108

112109
@Override
113-
public void close(VirtualChannel channel) throws IOException {
110+
public void close(VirtualChannel channel) throws IOException, InterruptedException {
114111
Throwable first = null;
115112
for (int index = keyMaterials.length - 1; index >= 0; index--) {
116113
try {
117114
if (keyMaterials[index] != null) {
118115
keyMaterials[index].close(channel);
119116
}
120117
} catch (Throwable e) {
121-
first = first == null ? e : first;
118+
if (first == null) {
119+
first = e;
120+
} else {
121+
first.addSuppressed(e);
122+
}
122123
}
123124
}
124125
if (first != null) {
125-
if (first instanceof IOException) {
126-
throw (IOException) first;
127-
} else if (first instanceof RuntimeException) {
128-
throw (RuntimeException) first;
126+
if (first instanceof IOException ioe) {
127+
throw ioe;
128+
} else if (first instanceof InterruptedException ie) {
129+
throw ie;
130+
} else if (first instanceof RuntimeException re) {
131+
throw re;
129132
} else {
130133
throw new IOException("Error closing credentials.", first);
131134
}

0 commit comments

Comments
 (0)