Skip to content

Commit 901c6ea

Browse files
authored
Add try-catch block to catch module loading errors. (#134)
1 parent d2e20e4 commit 901c6ea

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

src/main/java/cpw/mods/modlauncher/ModuleLayerHandler.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,18 @@
2323
import cpw.mods.jarhandling.SecureJar;
2424
import cpw.mods.modlauncher.api.IModuleLayerManager;
2525
import cpw.mods.modlauncher.api.NamedPath;
26+
import org.apache.logging.log4j.LogManager;
27+
import org.apache.logging.log4j.Logger;
2628

2729
import java.lang.module.Configuration;
2830
import java.lang.module.ModuleFinder;
31+
import java.lang.module.ResolutionException;
2932
import java.util.*;
3033
import java.util.function.BiFunction;
3134
import java.util.function.Consumer;
3235

3336
public final class ModuleLayerHandler implements IModuleLayerManager {
37+
private static final Logger LOGGER = LogManager.getLogger();
3438
record LayerInfo(ModuleLayer layer, ModuleClassLoader cl) {}
3539

3640
private record PathOrJar(NamedPath path, SecureJar jar) {
@@ -72,7 +76,15 @@ public LayerInfo buildLayer(final Layer layer, BiFunction<Configuration, List<Mo
7276
.map(PathOrJar::build)
7377
.toArray(SecureJar[]::new);
7478
final var targets = Arrays.stream(finder).map(SecureJar::name).toList();
75-
final var newConf = Configuration.resolveAndBind(JarModuleFinder.of(finder), Arrays.stream(layer.getParent()).map(completedLayers::get).map(li->li.layer().configuration()).toList(), ModuleFinder.of(), targets);
79+
final Configuration newConf;
80+
try {
81+
newConf = Configuration.resolveAndBind(JarModuleFinder.of(finder), Arrays.stream(layer.getParent()).map(completedLayers::get).map(li -> li.layer().configuration()).toList(), ModuleFinder.of(), targets);
82+
} catch (ResolutionException e) {
83+
// Catch and log errors when two mods have the same package name, or other module loading errors
84+
// This ensures they make it to the log file.
85+
LOGGER.error("Error while resolving modules.", e);
86+
throw e;
87+
}
7688
final var allParents = Arrays.stream(layer.getParent()).map(completedLayers::get).map(LayerInfo::layer).<ModuleLayer>mapMulti((moduleLayer, comp)-> {
7789
comp.accept(moduleLayer);
7890
moduleLayer.parents().forEach(comp);

0 commit comments

Comments
 (0)