Is it normal that `Job.cancel()` throws a `Cancel...
# ktor
m
Is it normal that
Job.cancel()
throws a
CancellationException
itself? Or is this a bug in Ktor?
Copy code
java.util.concurrent.CancellationException: Job was cancelled
    at io.ktor.client.engine.UtilsKt$attachToUserJob$cleanupHandler$1.invoke(Utils.kt:77)
    at io.ktor.client.engine.UtilsKt$attachToUserJob$cleanupHandler$1.invoke(Unknown Source:2)
    …
    at kotlinx.coroutines.JobSupport.cancel(JobSupport.kt:614)
    at kotlinx.coroutines.Job$DefaultImpls.cancel$default(Job.kt:164)
e
Looks like a bug.
1
m
@luke yes, but the call that's canceled should throw the exception, not the code that initiates the cancelation.
l
Does this not indicate that an instance of
Job
can throw a
CancellationException
when calling
cancel()
? https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-job/cancel.html
m
No, it doesn't
l
Nope, you're right, sorry. I have no idea then.
o
It must be a
kotlin.coroutines.cancellation.CancellationException
but was
java.util.concurrent.CancellationException
Something went wrong. I have this bug now
to work around this behavior I use this construction in HttpResponseValidator block:
Copy code
handleResponseExceptionWithRequest { exception, _ ->
    when (exception) {
        is java.util.concurrent.CancellationException -> throw kotlin.coroutines.cancellation.CancellationException(exception)
        else -> throw exception
    }
}
Maybe, the problem is in linking wrong exception type
258 Views