Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 1 | /* |
Aurimas Liutikas | 79e555e | 2021-05-17 17:41:41 +0000 | [diff] [blame] | 2 | * Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 3 | */ |
| 4 | |
Roman Elizarov | 0aad8f1 | 2019-03-01 12:08:43 +0300 | [diff] [blame] | 5 | @file:Suppress("FunctionName") |
| 6 | |
Roman Elizarov | 0950dfa | 2018-07-13 10:33:25 +0300 | [diff] [blame] | 7 | package kotlinx.coroutines |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 8 | |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 9 | /** |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 10 | * 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 Tolstopyatov | 149ba48 | 2018-09-24 20:28:02 +0300 | [diff] [blame] | 13 | * See [CoroutineExceptionHandler] |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 14 | */ |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 15 | public actual typealias CancellationException = java.util.concurrent.CancellationException |
| 16 | |
| 17 | /** |
Roman Elizarov | 0aad8f1 | 2019-03-01 12:08:43 +0300 | [diff] [blame] | 18 | * Creates a cancellation exception with a specified message and [cause]. |
| 19 | */ |
| 20 | @Suppress("FunctionName") |
| 21 | public actual fun CancellationException(message: String?, cause: Throwable?) : CancellationException = |
| 22 | CancellationException(message).apply { initCause(cause) } |
| 23 | |
| 24 | /** |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 25 | * 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 Tolstopyatov | 1f7b2d8 | 2018-10-09 15:57:51 +0300 | [diff] [blame] | 29 | internal actual class JobCancellationException public actual constructor( |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 30 | message: String, |
| 31 | cause: Throwable?, |
Roman Elizarov | e89cd68 | 2018-04-25 13:03:40 +0300 | [diff] [blame] | 32 | @JvmField internal actual val job: Job |
Roman Elizarov | d293639 | 2019-03-04 22:20:25 +0300 | [diff] [blame] | 33 | ) : CancellationException(message), CopyableThrowable<JobCancellationException> { |
Vsevolod Tolstopyatov | f5e63ca | 2018-04-12 19:59:56 +0300 | [diff] [blame] | 34 | |
| 35 | init { |
| 36 | if (cause != null) initCause(cause) |
| 37 | } |
| 38 | |
| 39 | override fun fillInStackTrace(): Throwable { |
| 40 | if (DEBUG) { |
| 41 | return super.fillInStackTrace() |
| 42 | } |
Vsevolod Tolstopyatov | cfb3ff9 | 2020-03-16 12:36:08 +0300 | [diff] [blame] | 43 | // Prevent Android <= 6.0 bug, #1866 |
| 44 | stackTrace = emptyArray() |
Vsevolod Tolstopyatov | f5e63ca | 2018-04-12 19:59:56 +0300 | [diff] [blame] | 45 | /* |
| 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 Elizarov | d293639 | 2019-03-04 22:20:25 +0300 | [diff] [blame] | 53 | 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 Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 64 | override fun toString(): String = "${super.toString()}; job=$job" |
Vsevolod Tolstopyatov | a2d8088 | 2018-09-24 19:51:49 +0300 | [diff] [blame] | 65 | |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 66 | 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 Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 73 | @Suppress("NOTHING_TO_INLINE") |
| 74 | internal actual inline fun Throwable.addSuppressedThrowable(other: Throwable) = |
Vsevolod Tolstopyatov | 6d1a6e3 | 2020-02-18 15:28:00 +0300 | [diff] [blame] | 75 | addSuppressed(other) |