Skip to content

Commit 472c782

Browse files
Backport to 1.21.1: Fix ParametrizedGameTestSequence swallowing exceptions (#2038)
Co-authored-by: sciwhiz12 <[email protected]>
1 parent 03ddf89 commit 472c782

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

testframework/src/main/java/net/neoforged/testframework/gametest/ParametrizedGameTestSequence.java

+21-1
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,25 @@ public ParametrizedGameTestSequence(GameTestInfo info, ExtendedSequence sequence
2424
this.info = info;
2525
this.sequence = sequence;
2626

27+
final AtomicReference<Throwable> capturedException = new AtomicReference<>();
2728
final AtomicReference<T> val = new AtomicReference<>();
28-
sequence.thenExecute(() -> val.set(value.get()));
29+
sequence.thenExecute(() -> {
30+
try {
31+
val.set(value.get());
32+
} catch (Throwable ex) {
33+
// Capture the exception to rethrow later, to avoid overwriting it with our own
34+
capturedException.set(ex);
35+
throw ex;
36+
}
37+
});
2938
this.value = () -> {
3039
final var v = val.get();
3140
if (v == null) {
41+
// Rethrow the captured exception if any before throwing our own exception
42+
final var ex = capturedException.get();
43+
if (ex != null) {
44+
sneakyThrow(ex);
45+
}
3246
throw new GameTestAssertException("Expected value to be non-null!");
3347
}
3448
return v;
@@ -137,4 +151,10 @@ public void thenFail(Function<T, Exception> exception) {
137151
public GameTestSequence.Condition thenTrigger() {
138152
return sequence.thenTrigger();
139153
}
154+
155+
// Never returns normally.
156+
@SuppressWarnings("unchecked")
157+
private static <E extends Throwable> void sneakyThrow(Throwable exception) throws E {
158+
throw (E) exception;
159+
}
140160
}

0 commit comments

Comments
 (0)