Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure that exception recovery does not break exception message #1700

Merged
merged 1 commit into from
Dec 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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")
}