diff options
author | hannibal2 <24389977+hannibal00212@users.noreply.github.com> | 2023-06-22 13:00:47 +0200 |
---|---|---|
committer | hannibal2 <24389977+hannibal00212@users.noreply.github.com> | 2023-06-22 13:00:47 +0200 |
commit | 3e7aa9855489888283b6c53ce14002fe38d04dbe (patch) | |
tree | 9300e8176ac357f0c5637209196dd65f4c287d5c /src/main/java/at | |
parent | 47ac0f99f9d482c3ab3baf794d9ebf03fbee3d6b (diff) | |
download | skyhanni-3e7aa9855489888283b6c53ce14002fe38d04dbe.tar.gz skyhanni-3e7aa9855489888283b6c53ce14002fe38d04dbe.tar.bz2 skyhanni-3e7aa9855489888283b6c53ce14002fe38d04dbe.zip |
better error handling
Diffstat (limited to 'src/main/java/at')
8 files changed, 94 insertions, 37 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/config/commands/Commands.kt b/src/main/java/at/hannibal2/skyhanni/config/commands/Commands.kt index ebcb72d28..59b850784 100644 --- a/src/main/java/at/hannibal2/skyhanni/config/commands/Commands.kt +++ b/src/main/java/at/hannibal2/skyhanni/config/commands/Commands.kt @@ -125,9 +125,9 @@ object Commands { } private fun createCommand(function: (Array<String>) -> Unit) = - object : ProcessCommandRunnable() { - override fun processCommand(sender: ICommandSender?, args: Array<out String>) { - function(args.asList().toTypedArray()) - } + object : ProcessCommandRunnable() { + override fun processCommand(sender: ICommandSender?, args: Array<out String>) { + function(args.asList().toTypedArray()) } + } }
\ No newline at end of file diff --git a/src/main/java/at/hannibal2/skyhanni/config/commands/SimpleCommand.java b/src/main/java/at/hannibal2/skyhanni/config/commands/SimpleCommand.java index 25e3ba97b..fa2c310de 100644 --- a/src/main/java/at/hannibal2/skyhanni/config/commands/SimpleCommand.java +++ b/src/main/java/at/hannibal2/skyhanni/config/commands/SimpleCommand.java @@ -1,5 +1,6 @@ package at.hannibal2.skyhanni.config.commands; +import at.hannibal2.skyhanni.test.command.CopyErrorCommand; import net.minecraft.command.CommandBase; import net.minecraft.command.ICommandSender; import net.minecraft.util.BlockPos; @@ -7,7 +8,7 @@ import net.minecraft.util.BlockPos; import java.util.List; /** - @author Moulberry + * @author Moulberry **/ public class SimpleCommand extends CommandBase { @@ -50,7 +51,11 @@ public class SimpleCommand extends CommandBase { @Override public void processCommand(ICommandSender sender, String[] args) { - runnable.processCommand(sender, args); + try { + runnable.processCommand(sender, args); + } catch (Throwable e) { + CopyErrorCommand.INSTANCE.logError(e, "Error while running command /" + commandName); + } } @Override diff --git a/src/main/java/at/hannibal2/skyhanni/data/EntityMovementData.kt b/src/main/java/at/hannibal2/skyhanni/data/EntityMovementData.kt index 30f88f48e..b5a2d2b92 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/EntityMovementData.kt +++ b/src/main/java/at/hannibal2/skyhanni/data/EntityMovementData.kt @@ -21,8 +21,6 @@ class EntityMovementData { } } - var tick = 0 - @SubscribeEvent fun onTick(event: TickEvent.ClientTickEvent) { if (!LorenzUtils.inSkyBlock) return diff --git a/src/main/java/at/hannibal2/skyhanni/data/MayorElection.kt b/src/main/java/at/hannibal2/skyhanni/data/MayorElection.kt index ddd208db1..7c4cfd7c1 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/MayorElection.kt +++ b/src/main/java/at/hannibal2/skyhanni/data/MayorElection.kt @@ -1,11 +1,10 @@ package at.hannibal2.skyhanni.data import at.hannibal2.skyhanni.SkyHanniMod +import at.hannibal2.skyhanni.config.ConfigManager import at.hannibal2.skyhanni.utils.APIUtil import at.hannibal2.skyhanni.utils.LorenzUtils import at.hannibal2.skyhanni.utils.jsonobjects.MayorJson -import com.google.gson.GsonBuilder -import io.github.moulberry.moulconfig.observer.PropertyTypeAdapterFactory import io.github.moulberry.notenoughupdates.util.SkyBlockTime import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -17,10 +16,6 @@ class MayorElection { private var tick = 0 private var lastUpdate = 0L - private val gson = GsonBuilder().setPrettyPrinting() - .registerTypeAdapterFactory(PropertyTypeAdapterFactory()) - .create() - companion object { var rawMayorData: MayorJson? = null var candidates = mapOf<Int, MayorJson.Candidate>() @@ -52,7 +47,7 @@ class MayorElection { SkyHanniMod.coroutineScope.launch { val url = "https://api.hypixel.net/resources/skyblock/election" val jsonObject = withContext(Dispatchers.IO) { APIUtil.getJSONResponse(url) } - rawMayorData = gson.fromJson(jsonObject, MayorJson::class.java) + rawMayorData = ConfigManager.gson.fromJson(jsonObject, MayorJson::class.java) val data = rawMayorData ?: return@launch val map = mutableMapOf<Int, MayorJson.Candidate>() map put data.mayor.election.getPairs() diff --git a/src/main/java/at/hannibal2/skyhanni/data/repo/RepoManager.kt b/src/main/java/at/hannibal2/skyhanni/data/repo/RepoManager.kt index 2dc65f358..e1d6aa933 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/repo/RepoManager.kt +++ b/src/main/java/at/hannibal2/skyhanni/data/repo/RepoManager.kt @@ -1,10 +1,9 @@ package at.hannibal2.skyhanni.data.repo import at.hannibal2.skyhanni.SkyHanniMod +import at.hannibal2.skyhanni.config.ConfigManager import at.hannibal2.skyhanni.events.RepositoryReloadEvent import at.hannibal2.skyhanni.utils.LorenzUtils -import com.google.gson.Gson -import com.google.gson.GsonBuilder import com.google.gson.JsonObject import net.minecraft.client.Minecraft import org.apache.commons.io.FileUtils @@ -15,7 +14,7 @@ import java.util.concurrent.CompletableFuture import java.util.concurrent.atomic.AtomicBoolean class RepoManager(private val configLocation: File) { - val gson: Gson = GsonBuilder().setPrettyPrinting().create() + private val gson get() = ConfigManager.gson private var latestRepoCommit: String? = null private val repoLocation: File = File(configLocation, "repo") diff --git a/src/main/java/at/hannibal2/skyhanni/data/repo/RepoUtils.kt b/src/main/java/at/hannibal2/skyhanni/data/repo/RepoUtils.kt index 3e67adcac..9719c70ff 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/repo/RepoUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/data/repo/RepoUtils.kt @@ -79,17 +79,13 @@ object RepoUtils { fun <T> getConstant(repo: File, constant: String, gson: Gson, clazz: Class<T>?): T? { if (repo.exists()) { val jsonFile = File(repo, "constants/$constant.json") - try { - BufferedReader( - InputStreamReader( - FileInputStream(jsonFile), - StandardCharsets.UTF_8 - ) - ).use { reader -> - return gson.fromJson(reader, clazz) - } - } catch (e: Exception) { - return null + BufferedReader( + InputStreamReader( + FileInputStream(jsonFile), + StandardCharsets.UTF_8 + ) + ).use { reader -> + return gson.fromJson(reader, clazz) } } return null diff --git a/src/main/java/at/hannibal2/skyhanni/events/RepositoryReloadEvent.kt b/src/main/java/at/hannibal2/skyhanni/events/RepositoryReloadEvent.kt index 5cfd59aa1..39d187326 100644 --- a/src/main/java/at/hannibal2/skyhanni/events/RepositoryReloadEvent.kt +++ b/src/main/java/at/hannibal2/skyhanni/events/RepositoryReloadEvent.kt @@ -1,6 +1,7 @@ package at.hannibal2.skyhanni.events import at.hannibal2.skyhanni.data.repo.RepoUtils +import at.hannibal2.skyhanni.test.command.CopyErrorCommand import com.google.gson.Gson import com.google.gson.JsonObject import java.io.File @@ -8,6 +9,10 @@ import java.io.File class RepositoryReloadEvent(val repoLocation: File, val gson: Gson): LorenzEvent() { fun getConstant(constant: String) = getConstant<JsonObject>(constant) - inline fun <reified T : Any> getConstant(constant: String) = + inline fun <reified T : Any> getConstant(constant: String) = try { RepoUtils.getConstant(repoLocation, constant, gson, T::class.java) + } catch (e: Exception) { + CopyErrorCommand.logError(Exception("Repo parsing error while trying to read constant '$constant'", e), "Error reading repo data") + null + } }
\ No newline at end of file diff --git a/src/main/java/at/hannibal2/skyhanni/test/command/CopyErrorCommand.kt b/src/main/java/at/hannibal2/skyhanni/test/command/CopyErrorCommand.kt index a8c6660f3..575a2e4fe 100644 --- a/src/main/java/at/hannibal2/skyhanni/test/command/CopyErrorCommand.kt +++ b/src/main/java/at/hannibal2/skyhanni/test/command/CopyErrorCommand.kt @@ -4,12 +4,14 @@ import at.hannibal2.skyhanni.SkyHanniMod import at.hannibal2.skyhanni.utils.LorenzUtils import at.hannibal2.skyhanni.utils.OSUtils import com.google.common.cache.CacheBuilder +import net.minecraft.client.Minecraft import java.util.* import java.util.concurrent.TimeUnit object CopyErrorCommand { // random id -> error message private val errorMessages = mutableMapOf<String, String>() + private val fullErrorMessages = mutableMapOf<String, String>() private var cache = CacheBuilder.newBuilder().expireAfterWrite(10, TimeUnit.MINUTES).build<Pair<String, Int>, Unit>() @@ -20,22 +22,34 @@ object CopyErrorCommand { return } - LorenzUtils.chat(errorMessages[array[0]]?.let { + val id = array[0] + val fullErrorMessage = LorenzUtils.isControlKeyDown() + val errorMessage = if (fullErrorMessage) { + fullErrorMessages[id] + } else { + errorMessages[id] + } + val name = if (fullErrorMessage) "Ful error" else "Error" + LorenzUtils.chat(errorMessage?.let { OSUtils.copyToClipboard(it) - "§e[SkyHanni] Error copied into the clipboard, please report it on the SkyHanni discord!" + "§e[SkyHanni] $name copied into the clipboard, please report it on the SkyHanni discord!" } ?: "§c[SkyHanni] Error id not found!") } fun logError(error: Throwable, message: String) { + Minecraft.getMinecraft().thePlayer ?: throw Error(message, error) + val pair = error.stackTrace[0].let { it.fileName to it.lineNumber } - if (cache.getIfPresent(pair) != null) return +// if (cache.getIfPresent(pair) != null) return cache.put(pair, Unit) - val stackTrace = error.stackTraceToString().removeSpam() + val fullStackTrace = error.getExactStackTrace(true).joinToString("\n") + val stackTrace = error.getExactStackTrace(false).joinToString("\n").removeSpam() val randomId = UUID.randomUUID().toString() - errorMessages[randomId] = - "```\nSkyHanni ${SkyHanniMod.version}: $message\n \n$stackTrace```" + errorMessages[randomId] = "```\nSkyHanni ${SkyHanniMod.version}: $message\n \n$stackTrace\n```" + fullErrorMessages[randomId] = + "```\nSkyHanni ${SkyHanniMod.version}: $message\n(full stack trace)\n \n$fullStackTrace\n```" LorenzUtils.clickableChat( "§c[SkyHanni ${SkyHanniMod.version}]: $message. Click here to copy the error into the clipboard.", @@ -44,12 +58,57 @@ object CopyErrorCommand { } } +private fun Throwable.getExactStackTrace(full: Boolean, parent: List<String> = emptyList()): List<String> = buildList { + add("Caused by " + javaClass.name + ": $message") + + val breakAfter = listOf( + "at net.minecraftforge.client.ClientCommandHandler.executeCommand(", + ) + val replace = mapOf( + "io.mouberry,notenoughupdates" to "NEU", + "at.hannibal2.skyhanni" to "SH", + ) + + for (traceElement in stackTrace) { + var text = "\tat $traceElement" + if (!full) { + if (text in parent) { + println("broke at: $text") + break + } + } + if (!full) { + for ((from, to) in replace) { + text = text.replace(from, to) + } + } + add(text) + if (!full) { + if (breakAfter.any { text.contains(it) }) { + println("breakAfter: $text") + break + } + } + } + + cause?.let { + addAll(it.getExactStackTrace(full, this)) + } +} + private fun String.removeSpam(): String { val ignored = listOf( "at io.netty.", "at net.minecraft.network.", "at net.minecraftforge.fml.common.network.handshake.", "at java.lang.Thread.run", + "at com.google.gson.internal.", + "at net.minecraftforge.fml.common.eventhandler.", + "at java.util.concurrent.", + "at sun.reflect.", + "at net.minecraft.client.Minecraft.addScheduledTask(", + "at java.lang.reflect.", + "at at.hannibal2.skyhanni.config.commands.Commands\$", ) - return split("\r\n\t").filter { line -> !ignored.any { line.startsWith(it) } }.joinToString("\n") + return split("\n").filter { line -> !ignored.any { line.contains(it) } }.joinToString("\n") } |