Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 1 | /* |
Roman Elizarov | 1f74a2d | 2018-06-29 19:19:45 +0300 | [diff] [blame] | 2 | * Copyright 2016-2018 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 | 0950dfa | 2018-07-13 10:33:25 +0300 | [diff] [blame] | 5 | package kotlinx.coroutines |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 6 | |
| 7 | /** |
| 8 | * This exception gets thrown if an exception is caught while processing [CompletionHandler] invocation for [Job]. |
Roman Elizarov | d826f5f | 2018-10-15 16:10:17 +0300 | [diff] [blame] | 9 | * |
| 10 | * @suppress **This an internal API and should not be used from general code.** |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 11 | */ |
Roman Elizarov | d826f5f | 2018-10-15 16:10:17 +0300 | [diff] [blame] | 12 | @InternalCoroutinesApi |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 13 | public actual class CompletionHandlerException public actual constructor( |
| 14 | message: String, |
| 15 | public override val cause: Throwable |
| 16 | ) : RuntimeException(message.withCause(cause)) |
| 17 | |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 18 | /** |
| 19 | * Thrown by cancellable suspending functions if the [Job] of the coroutine is cancelled while it is suspending. |
| 20 | * It indicates _normal_ cancellation of a coroutine. |
| 21 | * **It is not printed to console/log by default uncaught exception handler**. |
Vsevolod Tolstopyatov | 149ba48 | 2018-09-24 20:28:02 +0300 | [diff] [blame] | 22 | * (see [CoroutineExceptionHandler]). |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 23 | */ |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 24 | public actual open class CancellationException actual constructor(message: String?) : IllegalStateException(message) |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 25 | |
| 26 | /** |
Roman Elizarov | 0aad8f1 | 2019-03-01 12:08:43 +0300 | [diff] [blame] | 27 | * Creates a cancellation exception with a specified message and [cause]. |
| 28 | */ |
| 29 | @Suppress("FunctionName") |
| 30 | public actual fun CancellationException(message: String?, cause: Throwable?) : CancellationException = |
| 31 | CancellationException(message.withCause(cause)) |
| 32 | |
| 33 | /** |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 34 | * Thrown by cancellable suspending functions if the [Job] of the coroutine is cancelled or completed |
| 35 | * without cause, or with a cause or exception that is not [CancellationException] |
| 36 | * (see [Job.getCancellationException]). |
| 37 | */ |
Vsevolod Tolstopyatov | 1f7b2d8 | 2018-10-09 15:57:51 +0300 | [diff] [blame] | 38 | internal actual class JobCancellationException public actual constructor( |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 39 | message: String, |
| 40 | public override val cause: Throwable?, |
Roman Elizarov | e89cd68 | 2018-04-25 13:03:40 +0300 | [diff] [blame] | 41 | internal actual val job: Job |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 42 | ) : CancellationException(message.withCause(cause)) { |
| 43 | override fun toString(): String = "${super.toString()}; job=$job" |
| 44 | override fun equals(other: Any?): Boolean = |
| 45 | other === this || |
| 46 | other is JobCancellationException && other.message == message && other.job == job && other.cause == cause |
| 47 | override fun hashCode(): Int = |
| 48 | (message!!.hashCode() * 31 + job.hashCode()) * 31 + (cause?.hashCode() ?: 0) |
| 49 | } |
| 50 | |
Roman Elizarov | f29203c | 2018-01-11 12:39:36 +0300 | [diff] [blame] | 51 | @Suppress("FunctionName") |
| 52 | internal fun IllegalStateException(message: String, cause: Throwable?) = |
| 53 | IllegalStateException(message.withCause(cause)) |
| 54 | |
Roman Elizarov | 0aad8f1 | 2019-03-01 12:08:43 +0300 | [diff] [blame] | 55 | private fun String?.withCause(cause: Throwable?) = |
Roman Elizarov | 73b456b | 2019-03-06 17:18:27 +0300 | [diff] [blame] | 56 | when { |
| 57 | cause == null -> this |
| 58 | this == null -> "caused by $cause" |
| 59 | else -> "$this; caused by $cause" |
| 60 | } |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 61 | |
| 62 | @Suppress("NOTHING_TO_INLINE") |
Roman Elizarov | d293639 | 2019-03-04 22:20:25 +0300 | [diff] [blame] | 63 | internal actual inline fun Throwable.addSuppressedThrowable(other: Throwable) { /* empty */ } |
| 64 | |
| 65 | // For use in tests |
| 66 | internal actual val RECOVER_STACK_TRACES: Boolean = false |