diff --git a/src/dorkbox/network/connection/ListenerManager.kt b/src/dorkbox/network/connection/ListenerManager.kt index 6f727e61..e821cb2a 100644 --- a/src/dorkbox/network/connection/ListenerManager.kt +++ b/src/dorkbox/network/connection/ListenerManager.kt @@ -96,7 +96,7 @@ internal class ListenerManager(private val logger: KLogg } /** - * Remove from the stacktrace (going in reverse), kotlin coroutine info ONLY. This is for internal logs when a problem happens INSIDE the network stack + * Remove from the stacktrace kotlin coroutine info ONLY that is inside the network stack. 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. */ @@ -109,29 +109,18 @@ internal class ListenerManager(private val logger: KLogg return } - var newEndIndex = 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 - } + // we want to ONLY filter stuff that is past the highest dorkbox index AND is a coroutine. + val firstDorkboxIndex = stackTrace.indexOfFirst { it.className.startsWith("dorkbox.network.") } + val lastDorkboxIndex = stackTrace.indexOfLast { it.className.startsWith("dorkbox.network.") } + throwable.stackTrace = stackTrace.filterIndexed { index, element -> + val stackName = element.className + if (index <= firstDorkboxIndex && index >= lastDorkboxIndex) { + true + } else { + !(stackName.startsWith("kotlinx.coroutines.") || + stackName.startsWith("kotlin.coroutines.")) } - } - - 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) - } + }.toTypedArray() } } diff --git a/src/dorkbox/network/handshake/ClientHandshake.kt b/src/dorkbox/network/handshake/ClientHandshake.kt index 6ef539e8..f87f07c3 100644 --- a/src/dorkbox/network/handshake/ClientHandshake.kt +++ b/src/dorkbox/network/handshake/ClientHandshake.kt @@ -186,10 +186,13 @@ internal class ClientHandshake( ListenerManager.cleanStackTraceInternal(failedEx) throw failedEx } + if (connectionHelloInfo == null) { // no longer necessary to hold this connection open (if not a failure, we close the handshake after the DONE message) handshakeConnection.close() - throw ClientTimedOutException("Waiting for registration response from server") + val exception = ClientTimedOutException("Waiting for registration response from server") + ListenerManager.cleanStackTraceInternal(exception) + throw exception } return connectionHelloInfo!! @@ -203,7 +206,6 @@ internal class ClientHandshake( try { endPoint.writeHandshakeMessage(handshakeConnection.publication, registrationMessage) } catch (e: Exception) { - logger.error("Handshake error!", e) return false } @@ -247,7 +249,9 @@ internal class ClientHandshake( } if (!connectionDone) { - throw ClientTimedOutException("Waiting for registration response from server") + val exception = ClientTimedOutException("Waiting for registration response from server") + ListenerManager.cleanStackTraceInternal(exception) + throw exception } return connectionDone