Erik
05/07/2020, 10:31 AMCoroutineWorker
that does work on a coroutine. This work can be cancelled by the system, it then throws a CancellationException
.
In my apps I want to log any exceptions that are thrown by the crucial part of my work, e.g.: runCatching { crucialWork() }.onFailure { log(it) }
. However, I see a lot of JobCancellationException
instances being logged with message "Job was cancelled"
. I don't want to log regular cancellations without an exceptional cause. So, if I read the documentation of Job
(https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-job/index.html), am I right to conclude that I'm only interested in exceptions that are not of type CancellationException
, or if I catch a CancellationException
, then I'm only interested if its cause
is not a CancellationException
? In other words: I'm interested in 'failed' coroutines, not in 'normally cancelled' coroutines.
So, what exceptions do I log, what do I ignore?Fatih
05/07/2020, 11:01 AMFatih
05/07/2020, 11:03 AMFatih
05/07/2020, 11:06 AMErik
05/07/2020, 11:11 AMFatih
05/07/2020, 2:03 PMZach Klippenstein (he/him) [MOD]
05/07/2020, 7:01 PMCancellationException
. E.g.
catch (e: Throwable) {
val realCause = generateSequence(e) { e.cause }
.firstOrNull { it !is CancellationException }
?: throw e
// process realCause
}
Erik
05/07/2020, 7:02 PMRechee Jozil
05/07/2020, 8:51 PM