Skip to content

Nested exceptions are incorrectly recovered #3912

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

Closed
vadimsemenov opened this issue Oct 9, 2023 · 2 comments
Closed

Nested exceptions are incorrectly recovered #3912

vadimsemenov opened this issue Oct 9, 2023 · 2 comments

Comments

@vadimsemenov
Copy link
Contributor

See #3714 (comment)

This test asserts wrong behaviour.

Correct test:

        val result = runCatching {
            coroutineScope<Unit> {
                throw RuntimeException("outer", IllegalStateException("inner"))
            }
        }
        val ex = result.exceptionOrNull() ?: error("Expected to fail")
        assertIs<RuntimeException>(ex)
        assertEquals("outer", ex.message)
        val originalCause = ex?.cause
        assertIs<IllegalStateException>(originalCause)
        assertEquals("inner", originalCause.message)
@dkhalanskyjb
Copy link
Collaborator

RuntimeException does not implement CopyableThrowable, so it's not copied, but instead wrapped into a new RuntimeException. The original and the copy have different stack traces and object identities, and accessing the original is occasionally useful. This behavior is documented and intentional: https://github.com/Kotlin/kotlinx.coroutines/blob/master/docs/topics/debugging.md#stacktrace-recovery-machinery

stacktrace recovery machinery tries to create a copy of the original exception (with the original exception as the cause)

@dkhalanskyjb
Copy link
Collaborator

By the way, if you need to access the original exception regardless of whether stacktrace recovery was enabled, please describe your use cases in a reply to #2551 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants