diff options
| author | hannibal2 <24389977+hannibal00212@users.noreply.github.com> | 2023-10-21 11:16:34 +0200 |
|---|---|---|
| committer | hannibal2 <24389977+hannibal00212@users.noreply.github.com> | 2023-10-21 11:16:34 +0200 |
| commit | 830fdb7022ac173b421daeddfbc7e67c0d9f891b (patch) | |
| tree | a9e4397add1cc121e9cea0a35500de5ccd66f5a9 /src/main/java/at/hannibal2/skyhanni/events | |
| parent | 020c278b3c6cdff38449e0fe67bb36bf196ed045 (diff) | |
| download | skyhanni-830fdb7022ac173b421daeddfbc7e67c0d9f891b.tar.gz skyhanni-830fdb7022ac173b421daeddfbc7e67c0d9f891b.tar.bz2 skyhanni-830fdb7022ac173b421daeddfbc7e67c0d9f891b.zip | |
better repo error handling
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/events')
| -rw-r--r-- | src/main/java/at/hannibal2/skyhanni/events/LorenzEvent.kt | 41 | ||||
| -rw-r--r-- | src/main/java/at/hannibal2/skyhanni/events/RepositoryReloadEvent.kt | 13 |
2 files changed, 36 insertions, 18 deletions
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<out IEventListener> { + 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<JsonObject>(constant) - inline fun <reified T : Any> getConstant(constant: String, type: Type? = null) = try { + inline fun <reified T : Any> 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 +} |
