From 6dddc77fdcf79da6d5218e54c4e70d2941dc17d3 Mon Sep 17 00:00:00 2001 From: nathan Date: Fri, 4 Sep 2020 10:19:53 +0200 Subject: [PATCH] Internal stack traces should keep dorkbox network info --- .../network/connection/ListenerManager.kt | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/dorkbox/network/connection/ListenerManager.kt b/src/dorkbox/network/connection/ListenerManager.kt index 5eb4c9c8..03e7888f 100644 --- a/src/dorkbox/network/connection/ListenerManager.kt +++ b/src/dorkbox/network/connection/ListenerManager.kt @@ -70,6 +70,39 @@ internal class ListenerManager { throwable.stackTrace = stackTrace.copyOfRange(0, 1) } } + + /** + * Remove from the stacktrace (going in reverse), kotlin coroutine info ONLY. This is for internal logs when a problem happens INSIDE the network stack + * + * Neither of these are useful in resolving exception handling from a users perspective, and only clutter the stacktrace. + */ + fun cleanStackTraceInternal(throwable: Throwable) { + // NOTE: when we remove stuff, we ONLY want to remove the "tail" of the stacktrace, not ALL parts of the stacktrace + val stackTrace = throwable.stackTrace + var newEndIndex = stackTrace.size -1 // offset by 1 because we have to adjust for the access index + + for (i in newEndIndex downTo 0) { + val stackName = stackTrace[i].className + if (i == newEndIndex) { + if (stackName.startsWith("kotlinx.coroutines.") || + stackName.startsWith("kotlin.coroutines.")) { + newEndIndex-- + } else { + break + } + } + } + + newEndIndex++ // have to add 1 back, because a copy must be by size (and we access from 0) + + if (newEndIndex > 0) { + // newEndIndex will also remove the VERY LAST CachedMethod or CachedAsmMethod access invocation (because it's offset by 1) + throwable.stackTrace = stackTrace.copyOfRange(0, newEndIndex) + } else { + // keep just one, since it's a stack frame INSIDE our network library, and we need that! + throwable.stackTrace = stackTrace.copyOfRange(0, 1) + } + } } // initialize a emtpy arrays