Skip to content

Throw String in Kotlin/JS leads to ClassCastException #4290

Open
@ilgonmic

Description

Describe the bug

https://youtrack.jetbrains.com/issue/KT-72174/Kotlin-JS-Coroutines-throwing-JS-string-does-not-stop-the-job

JavaScript allows to throw strings, but it leads to ClassCastException is thrown in kotlinx-coroutines-core
Looks createCauseException is the root cause of ClassCastException , String is not Throwable so it is trying to be cast to ParentJob

// cause is Throwable or ParentJob when cancelChild was invoked
private fun createCauseException(cause: Any?): Throwable = when (cause) {
is Throwable? -> cause ?: defaultCancellationException()
else -> (cause as ParentJob).getChildJobCancellationCause()
}

Provide a Reproducer

class TestClient2 {
    @Test
    fun testThrowString() = runTest {
        val job = launch {
            println("throwing a JS string")
            throw IllegalStateException("EMPTY") //In JS everything can be thrown, even a String!
            println("not executed....")
        }

        println("waiting for job")
        job.join()
    }
}

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions