Skip to content

Commit

Permalink
Make sure that exception recovery does not break exception message
Browse files Browse the repository at this point in the history
Fixes #1631
  • Loading branch information
elizarov committed Dec 12, 2019
1 parent af9a201 commit 04e587c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
Expand Up @@ -66,14 +66,15 @@ private fun <E : Throwable> recoverFromStackFrame(exception: E, continuation: Co

// Try to create an exception of the same type and get stacktrace from continuation
val newException = tryCopyException(cause) ?: return exception
// Verify that the new exception has the same message as the original one (bail out if not, see #1631)
if (newException.message != cause.message) return exception
// Update stacktrace
val stacktrace = createStackTrace(continuation)
if (stacktrace.isEmpty()) return exception

// Merge if necessary
if (cause !== exception) {
mergeRecoveredTraces(recoveredStacktrace, stacktrace)
}

// Take recovered stacktrace, merge it with existing one if necessary and return
return createFinalException(cause, newException, stacktrace)
}
Expand Down
Expand Up @@ -8,6 +8,7 @@ import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
import kotlinx.coroutines.intrinsics.*
import org.junit.Test
import java.lang.RuntimeException
import java.util.concurrent.*
import kotlin.concurrent.*
import kotlin.coroutines.*
Expand Down Expand Up @@ -264,4 +265,18 @@ class StackTraceRecoveryTest : TestBase() {
}
yield() // nop to make sure it is not a tail call
}

@Test
fun testWrongMessageException() = runTest {
val result = runCatching {
coroutineScope<Unit> {
throw WrongMessageException("OK")
}
}
val ex = result.exceptionOrNull() ?: error("Expected to fail")
assertTrue(ex is WrongMessageException)
assertEquals("Token OK", ex.message)
}

public class WrongMessageException(token: String) : RuntimeException("Token $token")
}

0 comments on commit 04e587c

Please sign in to comment.