From 830fdb7022ac173b421daeddfbc7e67c0d9f891b Mon Sep 17 00:00:00 2001 From: hannibal2 <24389977+hannibal00212@users.noreply.github.com> Date: Sat, 21 Oct 2023 11:16:34 +0200 Subject: better repo error handling --- .../at/hannibal2/skyhanni/events/LorenzEvent.kt | 41 ++++++++++++++++------ .../skyhanni/events/RepositoryReloadEvent.kt | 13 +++---- 2 files changed, 36 insertions(+), 18 deletions(-) (limited to 'src/main/java/at/hannibal2/skyhanni/events') diff --git a/src/main/java/at/hannibal2/skyhanni/events/LorenzEvent.kt b/src/main/java/at/hannibal2/skyhanni/events/LorenzEvent.kt index 32c552221..e4e6f87c5 100644 --- a/src/main/java/at/hannibal2/skyhanni/events/LorenzEvent.kt +++ b/src/main/java/at/hannibal2/skyhanni/events/LorenzEvent.kt @@ -1,8 +1,10 @@ package at.hannibal2.skyhanni.events +import at.hannibal2.skyhanni.mixins.transformers.AccessorEventBus import at.hannibal2.skyhanni.test.command.ErrorManager import net.minecraftforge.common.MinecraftForge import net.minecraftforge.fml.common.eventhandler.Event +import net.minecraftforge.fml.common.eventhandler.IEventListener abstract class LorenzEvent : Event() { @@ -10,16 +12,35 @@ abstract class LorenzEvent : Event() { this::class.simpleName } - fun postAndCatch(): Boolean { - return runCatching { - postWithoutCatch() - }.onFailure { - ErrorManager.logError( - it, - "Caught an ${it::class.simpleName ?: "error"} at ${eventName}: '${it.message}'" - ) - }.getOrDefault(isCanceled) + fun postAndCatch() = postAndCatchAndBlock {} + + fun postAndCatchAndBlock( + printError: Boolean = true, + stopOnError: Boolean = false, + onError: (Throwable) -> Unit, + ): Boolean { + for (listener in getListeners()) { + try { + listener.invoke(this) + } catch (e: Throwable) { + if (printError) { + val callerName = listener.toString().split(" ")[1].split("@")[0].split(".").last() + ErrorManager.logError( + e, + "Caught an ${e::class.simpleName ?: "error"} at $eventName in $callerName: '${e.message}'" + ) + } + onError(e) + if (stopOnError) break + } + } + return if (isCancelable) isCanceled else false + } + + private fun getListeners(): Array { + val accessorEventBus = MinecraftForge.EVENT_BUS as AccessorEventBus + return listenerList.getListeners(accessorEventBus.busId) } fun postWithoutCatch() = MinecraftForge.EVENT_BUS.post(this) -} \ No newline at end of file +} diff --git a/src/main/java/at/hannibal2/skyhanni/events/RepositoryReloadEvent.kt b/src/main/java/at/hannibal2/skyhanni/events/RepositoryReloadEvent.kt index 8010564a0..e0eb01b9b 100644 --- a/src/main/java/at/hannibal2/skyhanni/events/RepositoryReloadEvent.kt +++ b/src/main/java/at/hannibal2/skyhanni/events/RepositoryReloadEvent.kt @@ -1,7 +1,7 @@ package at.hannibal2.skyhanni.events +import at.hannibal2.skyhanni.data.repo.RepoError import at.hannibal2.skyhanni.data.repo.RepoUtils -import at.hannibal2.skyhanni.test.command.ErrorManager import com.google.gson.Gson import com.google.gson.JsonObject import java.io.File @@ -10,13 +10,10 @@ import java.lang.reflect.Type class RepositoryReloadEvent(val repoLocation: File, val gson: Gson) : LorenzEvent() { fun getConstant(constant: String) = getConstant(constant) - inline fun getConstant(constant: String, type: Type? = null) = try { + inline fun getConstant(constant: String, type: Type? = null): T = try { + if (!repoLocation.exists()) throw RepoError("Repo folder does not exist!") RepoUtils.getConstant(repoLocation, constant, gson, T::class.java, type) } catch (e: Exception) { - ErrorManager.logError( - Exception("Repo parsing error while trying to read constant '$constant'", e), - "Error reading repo data" - ) - null + throw RepoError("Repo parsing error while trying to read constant '$constant'", e) } -} \ No newline at end of file +} -- cgit