Skip to content

Commit 1d2dbaa

Browse files
committed
Clean up chaining suppressed exceptions
1 parent 67380cb commit 1d2dbaa

2 files changed

Lines changed: 23 additions & 17 deletions

File tree

src/main/java/io/github/notstirred/chunkyeditor/state/vanilla/VanillaStateTracker.java

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import io.github.notstirred.chunkyeditor.VanillaRegionPos;
44
import io.github.notstirred.chunkyeditor.state.State;
5+
import io.github.notstirred.chunkyeditor.util.ExceptionUtils;
56
import se.llbit.util.Pair;
67
import se.llbit.util.annotation.NotNull;
78
import se.llbit.util.annotation.Nullable;
@@ -115,7 +116,7 @@ private StateGroup snapshot(Collection<VanillaRegionPos> regionPositions) throws
115116
*/
116117
@NotNull
117118
private Pair<StateGroup, IOException> snapshotNoFail(Collection<VanillaRegionPos> regionPositions) {
118-
IOException suppressedExceptions = null;
119+
Collection<IOException> suppressedExceptions = new ArrayList<>();
119120

120121
StateGroup states = new StateGroup();
121122
if (this.currentStateIdx == NO_STATE) {
@@ -124,11 +125,7 @@ private Pair<StateGroup, IOException> snapshotNoFail(Collection<VanillaRegionPos
124125
try {
125126
states.put(regionPos, externalStateForRegion(regionPos));
126127
} catch (IOException e) {
127-
if (suppressedExceptions == null) {
128-
suppressedExceptions = e;
129-
} else {
130-
suppressedExceptions.addSuppressed(e);
131-
}
128+
suppressedExceptions.add(e);
132129
states.put(regionPos, null);
133130
}
134131
}
@@ -145,11 +142,7 @@ private Pair<StateGroup, IOException> snapshotNoFail(Collection<VanillaRegionPos
145142
try {
146143
externalState = externalStateForRegion(regionPos);
147144
} catch (IOException e) {
148-
if (suppressedExceptions == null) {
149-
suppressedExceptions = e;
150-
} else {
151-
suppressedExceptions.addSuppressed(e);
152-
}
145+
suppressedExceptions.add(e);
153146
continue; // we failed to snapshot this region, continue to the next ones.
154147
}
155148

@@ -164,17 +157,13 @@ private Pair<StateGroup, IOException> snapshotNoFail(Collection<VanillaRegionPos
164157
}
165158
}
166159
} catch (IOException e) {
167-
if (suppressedExceptions == null) {
168-
suppressedExceptions = e;
169-
} else {
170-
suppressedExceptions.addSuppressed(e);
171-
}
160+
suppressedExceptions.add(e);
172161
}
173162
}
174163
states.put(regionPos, externalState);
175164
}
176165
}
177-
return new Pair<>(states, suppressedExceptions);
166+
return new Pair<>(states, ExceptionUtils.chainSuppressedExceptions(suppressedExceptions));
178167
}
179168

180169
/**
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package io.github.notstirred.chunkyeditor.util;
2+
3+
import java.util.Collection;
4+
5+
public class ExceptionUtils {
6+
public static <T extends Exception> T chainSuppressedExceptions(Collection<? extends T> causes) {
7+
T first = null;
8+
for (T cause : causes) {
9+
if (first == null) {
10+
first = cause;
11+
} else {
12+
first.addSuppressed(cause);
13+
}
14+
}
15+
return first;
16+
}
17+
}

0 commit comments

Comments
 (0)