Skip to content

Thrown exceptions incorrectly wrapped in a new instance of the same class when constructor with Throwable param is provided #5067

@kslesinski-godaddy

Description

@kslesinski-godaddy

Exceptions thrown from kotlinx.coroutines.rx3.rxSingle.await() that have a constructor with a Throwable param are being wrapped in a new instance of the same class. This is only happening when code is executed from within a @org.junit.jupiter.api.Test. The expected behaviour, I assume, is for the thrown exception to be delivered as-is, as it is done when the code is executed from main().

Steps to reproduce

Expected/Actual output:

org.example.ExceptionWithThrowableParam: java.lang.Exception
java.lang.Exception

Main.kt

package org.example

import kotlinx.coroutines.rx3.await
import kotlinx.coroutines.rx3.rxSingle

suspend fun main() {
    repro()
}

suspend fun repro() {
    try {
        rxSingle {
            throw ExceptionWithThrowableParam(Exception())
        }.await()
    } catch (e: Exception) {
        println(e) // org.example.ExceptionWithThrowableParam: java.lang.Exception
        println(e.cause) // java.lang.Exception
    }
}

class ExceptionWithThrowableParam(inner: Throwable) : Exception(inner) // this works in `main()`, but fails in `test()`
//class ExceptionWithThrowableParam(inner: Exception) : Exception(inner) // this works fine in `main()` and `test()`

Actual output when run within @Test:

org.example.ExceptionWithThrowableParam: org.example.ExceptionWithThrowableParam: java.lang.Exception
org.example.ExceptionWithThrowableParam: java.lang.Exception

Test.kt

import kotlinx.coroutines.test.runTest
import org.example.repro
import kotlin.test.Test

class Test {
    @Test
    fun test() = runTest {
        repro()
    }
}

build.gradle.kts

plugins {
    kotlin("jvm") version "2.2.20"
}

group = "org.example"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-rx3:1.10.2")
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.10.2")
    implementation("io.reactivex.rxjava3:rxjava:3.1.1")
    testImplementation(kotlin("test"))
}

tasks.test {
    useJUnitPlatform()
}
kotlin {
    jvmToolchain(21)
}

Context

JUnit4 / 5

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions