From c4e4985b8b96ed156851c6be6d4a8d5e110c3040 Mon Sep 17 00:00:00 2001 From: Linnea Gräf Date: Fri, 13 Dec 2024 17:42:27 +0100 Subject: Add error reporting framework --- .../utils/telemetry/ExceptionContextValue.kt | 38 ++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/main/kotlin/moe/nea/ledger/utils/telemetry/ExceptionContextValue.kt (limited to 'src/main/kotlin/moe/nea/ledger/utils/telemetry/ExceptionContextValue.kt') diff --git a/src/main/kotlin/moe/nea/ledger/utils/telemetry/ExceptionContextValue.kt b/src/main/kotlin/moe/nea/ledger/utils/telemetry/ExceptionContextValue.kt new file mode 100644 index 0000000..df588a8 --- /dev/null +++ b/src/main/kotlin/moe/nea/ledger/utils/telemetry/ExceptionContextValue.kt @@ -0,0 +1,38 @@ +package moe.nea.ledger.utils.telemetry + +import com.google.gson.JsonArray +import com.google.gson.JsonElement +import com.google.gson.JsonObject + +class ExceptionContextValue(val exception: Throwable) : ContextValue { + val stackTrace by lazy { + exception.stackTraceToString() + } + + override fun serialize(): JsonElement { + val jsonObject = JsonObject() + jsonObject.addProperty("exception_stackTrace", stackTrace) + jsonObject.add("exception_structure", walkExceptions(exception, 6)) + return jsonObject + } + + private fun walkExceptions(exception: Throwable, searchDepth: Int): JsonElement { + val obj = JsonObject() + obj.addProperty("class", exception.javaClass.name) + obj.addProperty("message", exception.message) + // TODO: allow exceptions to implement an "extra info" interface + if (searchDepth > 0) { + if (exception.cause != null) { + obj.add("cause", walkExceptions(exception, searchDepth - 1)) + } + val suppressions = JsonArray() + for (suppressedException in exception.suppressedExceptions) { + suppressions.add(walkExceptions(suppressedException, searchDepth - 1)) + } + if (suppressions.size() > 0) { + obj.add("suppressions", suppressions) + } + } + return obj + } +} -- cgit