aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2024-11-13 15:44:04 +0100
committerLinnea Gräf <nea@nea.moe>2024-11-15 18:40:19 +0100
commit59566bcab8a5861e0cb78e8c207884006828bd63 (patch)
tree4ce3b19b3ab1f78652d5d805d8b7f2def16a1167 /src/main
parent57cdcb21ec3aa6f4d4f4ab07a3e4e8fd7d32f28d (diff)
downloadFirmament-59566bcab8a5861e0cb78e8c207884006828bd63.tar.gz
Firmament-59566bcab8a5861e0cb78e8c207884006828bd63.tar.bz2
Firmament-59566bcab8a5861e0cb78e8c207884006828bd63.zip
fix: Improve exception logging for entity widgets
Diffstat (limited to 'src/main')
-rw-r--r--src/main/kotlin/gui/entity/EntityRenderer.kt19
-rw-r--r--src/main/kotlin/util/ErrorUtil.kt25
2 files changed, 32 insertions, 12 deletions
diff --git a/src/main/kotlin/gui/entity/EntityRenderer.kt b/src/main/kotlin/gui/entity/EntityRenderer.kt
index ddb862f..022b9a3 100644
--- a/src/main/kotlin/gui/entity/EntityRenderer.kt
+++ b/src/main/kotlin/gui/entity/EntityRenderer.kt
@@ -3,7 +3,6 @@ package moe.nea.firmament.gui.entity
import com.google.gson.Gson
import com.google.gson.JsonArray
import com.google.gson.JsonObject
-import org.apache.logging.log4j.LogManager
import org.joml.Quaternionf
import org.joml.Vector3f
import kotlin.math.atan
@@ -15,8 +14,8 @@ import net.minecraft.entity.LivingEntity
import net.minecraft.entity.SpawnReason
import net.minecraft.util.Identifier
import net.minecraft.world.World
+import moe.nea.firmament.util.ErrorUtil
import moe.nea.firmament.util.MC
-import moe.nea.firmament.util.assertNotNullOr
import moe.nea.firmament.util.iterate
import moe.nea.firmament.util.openFirmamentResource
import moe.nea.firmament.util.render.enableScissorWithTranslation
@@ -76,18 +75,15 @@ object EntityRenderer {
"name" to ModifyName,
)
- val logger = LogManager.getLogger("Firmament.Entity")
fun applyModifiers(entityId: String, modifiers: List<JsonObject>): LivingEntity? {
- val entityType = assertNotNullOr(entityIds[entityId]) {
- logger.error("Could not create entity with id $entityId")
+ val entityType = ErrorUtil.notNullOr(entityIds[entityId], "Could not create entity with id $entityId") {
return null
}
- var entity = entityType()
+ var entity = ErrorUtil.catch("") { entityType() }.or { return null }
for (modifierJson in modifiers) {
- val modifier = assertNotNullOr(modifierJson["type"]?.asString?.let(entityModifiers::get)) {
- logger.error("Unknown modifier $modifierJson")
- return null
- }
+ val modifier = ErrorUtil.notNullOr(
+ modifierJson["type"]?.asString?.let(entityModifiers::get),
+ "Could not create entity with id $entityId. Failed to apply modifier $modifierJson") { return null }
entity = modifier.apply(entity, modifierJson)
}
return entity
@@ -95,8 +91,7 @@ object EntityRenderer {
fun constructEntity(info: JsonObject): LivingEntity? {
val modifiers = (info["modifiers"] as JsonArray?)?.map { it.asJsonObject } ?: emptyList()
- val entityType = assertNotNullOr(info["entity"]?.asString) {
- logger.error("Missing entity type on entity object")
+ val entityType = ErrorUtil.notNullOr(info["entity"]?.asString, "Missing entity type on entity object") {
return null
}
return applyModifiers(entityType, modifiers)
diff --git a/src/main/kotlin/util/ErrorUtil.kt b/src/main/kotlin/util/ErrorUtil.kt
index 5dc44d3..190381d 100644
--- a/src/main/kotlin/util/ErrorUtil.kt
+++ b/src/main/kotlin/util/ErrorUtil.kt
@@ -37,6 +37,31 @@ object ErrorUtil {
else Firmament.logger.error(message)
}
+ class Catch<T> private constructor(val value: T?, val exc: Throwable?) {
+ inline fun or(block: (exc: Throwable) -> T): T {
+ contract {
+ callsInPlace(block, InvocationKind.AT_MOST_ONCE)
+ }
+ if (exc != null) return block(exc)
+ @Suppress("UNCHECKED_CAST")
+ return value as T
+ }
+
+ companion object {
+ fun <T> fail(exception: Throwable): Catch<T> = Catch(null, exception)
+ fun <T> succeed(value: T): Catch<T> = Catch(value, null)
+ }
+ }
+
+ inline fun <T> catch(message: String, block: () -> T): Catch<T> {
+ try {
+ return Catch.succeed(block())
+ } catch (exc: Throwable) {
+ softError(message, exc)
+ return Catch.fail(exc)
+ }
+ }
+
inline fun <T : Any> notNullOr(nullable: T?, message: String, orElse: () -> T): T {
contract {
callsInPlace(orElse, InvocationKind.AT_MOST_ONCE)