From 792e103664e2a59b0e5c710582ad98cae4bb7a42 Mon Sep 17 00:00:00 2001 From: hannibal2 <24389977+hannibal00212@users.noreply.github.com> Date: Thu, 18 May 2023 15:41:09 +0200 Subject: Reworked clickable error messages and added support for all custom event errors --- .../skyhanni/test/command/CopyErrorCommand.kt | 47 ++++++++++++++++------ 1 file changed, 35 insertions(+), 12 deletions(-) (limited to 'src/main/java/at/hannibal2/skyhanni/test/command') 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 e2fbd98c5..6820deba7 100644 --- a/src/main/java/at/hannibal2/skyhanni/test/command/CopyErrorCommand.kt +++ b/src/main/java/at/hannibal2/skyhanni/test/command/CopyErrorCommand.kt @@ -1,21 +1,44 @@ package at.hannibal2.skyhanni.test.command +import at.hannibal2.skyhanni.SkyHanniMod import at.hannibal2.skyhanni.utils.LorenzUtils import at.hannibal2.skyhanni.utils.OSUtils +import com.google.common.cache.CacheBuilder +import java.util.* +import java.util.concurrent.TimeUnit object CopyErrorCommand { - var errorMessage = "" - var errorStackTrace = listOf() - fun command() { - try { - if (errorMessage == "") LorenzUtils.chat("§c[SkyHanni] no error to copy") else { - val result = errorMessage + "\nCaused at:\n" + errorStackTrace.joinToString("\n") - OSUtils.copyToClipboard(result) - LorenzUtils.chat("§e[SkyHanni] error message copied into the clipboard, please report it on the SkyHanni discord!") - } - } catch (error: Throwable) { - OSUtils.copyToClipboard(error.toString()) - LorenzUtils.chat("§c[SkyHanni] error occurred while fetching error, please report this on the SkyHanni discord!") + // random id -> error message + private val errorMessages = mutableMapOf() + private var cache = + CacheBuilder.newBuilder().expireAfterWrite(10, TimeUnit.MINUTES).build, Unit>() + + fun command(array: Array) { + if (array.size != 1) { + LorenzUtils.chat("§cUse /shcopyerror ") + + return } + + LorenzUtils.chat(errorMessages[array[0]]?.let { + OSUtils.copyToClipboard(it) + "§e[SkyHanni] Error copied into the clipboard, please report it on the SkyHanni discord!" + } ?: "§c[SkyHanni] Error id not found!") + } + + fun logError(error: Throwable, message: String) { + val pair = error.stackTrace[0].let { it.fileName to it.lineNumber } + if (cache.getIfPresent(pair) != null) return + cache.put(pair, Unit) + + val errorMessage = error.toString() + val stackTrace = error.stackTrace.asList().joinToString("\n") + val randomId = UUID.randomUUID().toString() + errorMessages[randomId] = "```$errorMessage\nCaused at:\n$stackTrace```" + + LorenzUtils.clickableChat( + "§cSkyHanni ${SkyHanniMod.version} $message. Click here to copy the error into the clipboard.", + "shcopyerror $randomId" + ) } } \ No newline at end of file -- cgit