blob: 007a0c98fac3d6f683afc603ff898b9178337587 [file] [log] [blame]
Roman Elizarovf29203c2018-01-11 12:39:36 +03001/*
Aurimas Liutikas79e555e2021-05-17 17:41:41 +00002 * Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
Roman Elizarovf29203c2018-01-11 12:39:36 +03003 */
4
Roman Elizarov0aad8f12019-03-01 12:08:43 +03005@file:Suppress("FunctionName")
6
Roman Elizarov0950dfa2018-07-13 10:33:25 +03007package kotlinx.coroutines
Roman Elizarovf29203c2018-01-11 12:39:36 +03008
Roman Elizarovf29203c2018-01-11 12:39:36 +03009/**
Roman Elizarovaa461cf2018-04-11 13:20:29 +030010 * Thrown by cancellable suspending functions if the [Job] of the coroutine is cancelled while it is suspending.
11 * It indicates _normal_ cancellation of a coroutine.
12 * **It is not printed to console/log by default uncaught exception handler**.
Vsevolod Tolstopyatov149ba482018-09-24 20:28:02 +030013 * See [CoroutineExceptionHandler]
Roman Elizarovaa461cf2018-04-11 13:20:29 +030014*/
Roman Elizarovf29203c2018-01-11 12:39:36 +030015public actual typealias CancellationException = java.util.concurrent.CancellationException
16
17/**
Roman Elizarov0aad8f12019-03-01 12:08:43 +030018 * Creates a cancellation exception with a specified message and [cause].
19 */
20@Suppress("FunctionName")
21public actual fun CancellationException(message: String?, cause: Throwable?) : CancellationException =
22 CancellationException(message).apply { initCause(cause) }
23
24/**
Roman Elizarovf29203c2018-01-11 12:39:36 +030025 * Thrown by cancellable suspending functions if the [Job] of the coroutine is cancelled or completed
26 * without cause, or with a cause or exception that is not [CancellationException]
27 * (see [Job.getCancellationException]).
28 */
Vsevolod Tolstopyatov1f7b2d82018-10-09 15:57:51 +030029internal actual class JobCancellationException public actual constructor(
Roman Elizarovf29203c2018-01-11 12:39:36 +030030 message: String,
31 cause: Throwable?,
Roman Elizarove89cd682018-04-25 13:03:40 +030032 @JvmField internal actual val job: Job
Roman Elizarovd2936392019-03-04 22:20:25 +030033) : CancellationException(message), CopyableThrowable<JobCancellationException> {
Vsevolod Tolstopyatovf5e63ca2018-04-12 19:59:56 +030034
35 init {
36 if (cause != null) initCause(cause)
37 }
38
39 override fun fillInStackTrace(): Throwable {
40 if (DEBUG) {
41 return super.fillInStackTrace()
42 }
Vsevolod Tolstopyatovcfb3ff92020-03-16 12:36:08 +030043 // Prevent Android <= 6.0 bug, #1866
44 stackTrace = emptyArray()
Vsevolod Tolstopyatovf5e63ca2018-04-12 19:59:56 +030045 /*
46 * In non-debug mode we don't want to have a stacktrace on every cancellation/close,
47 * parent job reference is enough. Stacktrace of JCE is not needed most of the time (e.g., it is not logged)
48 * and hurts performance.
49 */
50 return this
51 }
52
Roman Elizarovd2936392019-03-04 22:20:25 +030053 override fun createCopy(): JobCancellationException? {
54 if (DEBUG) {
55 return JobCancellationException(message!!, this, job)
56 }
57
58 /*
59 * In non-debug mode we don't copy JCE for speed as it does not have the stack trace anyway.
60 */
61 return null
62 }
63
Roman Elizarovf29203c2018-01-11 12:39:36 +030064 override fun toString(): String = "${super.toString()}; job=$job"
Vsevolod Tolstopyatova2d80882018-09-24 19:51:49 +030065
Roman Elizarovf29203c2018-01-11 12:39:36 +030066 override fun equals(other: Any?): Boolean =
67 other === this ||
68 other is JobCancellationException && other.message == message && other.job == job && other.cause == cause
69 override fun hashCode(): Int =
70 (message!!.hashCode() * 31 + job.hashCode()) * 31 + (cause?.hashCode() ?: 0)
71}
72
Roman Elizarovaa461cf2018-04-11 13:20:29 +030073@Suppress("NOTHING_TO_INLINE")
74internal actual inline fun Throwable.addSuppressedThrowable(other: Throwable) =
Vsevolod Tolstopyatov6d1a6e32020-02-18 15:28:00 +030075 addSuppressed(other)