[1.20.1] Improve mod loading error message for errors inside mod constructors (#9707)

This commit is contained in:
Paint_Ninja 2023-09-28 08:20:40 +01:00 committed by GitHub
parent 2fb2bbeed0
commit 48d72fb7c6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 0 deletions

View file

@ -21,6 +21,8 @@ import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;
import java.lang.reflect.InvocationTargetException;
import java.util.Objects;
import java.util.Optional;
public class FMLModContainer extends ModContainer
@ -70,6 +72,11 @@ public class FMLModContainer extends ModContainer
}
catch (Throwable e)
{
// When a mod constructor throws an exception, it's wrapped in an InvocationTargetException which hides the
// actual exception from the mod loading error screen.
if (e instanceof InvocationTargetException wrapped)
e = Objects.requireNonNullElse(wrapped.getCause(), e); // unwrap the exception
LOGGER.error(LOADING,"Failed to create mod instance. ModID: {}, class {}", getModId(), modClass.getName(), e);
throw new ModLoadingException(modInfo, ModLoadingStage.CONSTRUCT, "fml.modloading.failedtoloadmod", e, modClass);
}

View file

@ -0,0 +1,19 @@
/*
* Copyright (c) Forge Development LLC and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/
package net.minecraftforge.debug;
import net.minecraftforge.fml.common.Mod;
/**
* Test that the mod loading error screen shows the cause of the crash when a mod constructor throws an exception.
* <p>Enable or disable this test in the mods.toml</p>
*/
@Mod("constructor_throw_test")
public class ConstructorThrowTest {
public ConstructorThrowTest() {
throw new RuntimeException("Test");
}
}

View file

@ -41,6 +41,9 @@ modId="mdk_datagen"
[[mods]]
modId="calculate_normals_test"
#[[mods]]
# modId="constructor_throw_test"
# LEGACY TEST CASES
###### The mods below are from the old test framework and need to be yeeted later again.