blob: f42704107b8d42d1d306bef698972cb7977c64bf [file] [log] [blame]
Roman Elizarovf29203c2018-01-11 12:39:36 +03001/*
Roman Elizarov1f74a2d2018-06-29 19:19:45 +03002 * Copyright 2016-2018 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 Elizarov0950dfa2018-07-13 10:33:25 +03005package kotlinx.coroutines
Roman Elizarovf29203c2018-01-11 12:39:36 +03006
7/**
8 * This exception gets thrown if an exception is caught while processing [CompletionHandler] invocation for [Job].
Roman Elizarovd826f5f2018-10-15 16:10:17 +03009 *
10 * @suppress **This an internal API and should not be used from general code.**
Roman Elizarovf29203c2018-01-11 12:39:36 +030011 */
Roman Elizarovd826f5f2018-10-15 16:10:17 +030012@InternalCoroutinesApi
Roman Elizarovf29203c2018-01-11 12:39:36 +030013public actual class CompletionHandlerException public actual constructor(
14 message: String,
15 public override val cause: Throwable
16) : RuntimeException(message.withCause(cause))
17
Roman Elizarovaa461cf2018-04-11 13:20:29 +030018/**
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 Tolstopyatov149ba482018-09-24 20:28:02 +030022 * (see [CoroutineExceptionHandler]).
Roman Elizarovaa461cf2018-04-11 13:20:29 +030023 */
Vsevolod Tolstopyatov96191342018-04-20 18:13:33 +030024public actual open class CancellationException actual constructor(message: String?) : IllegalStateException(message)
Roman Elizarovf29203c2018-01-11 12:39:36 +030025
26/**
Roman Elizarov0aad8f12019-03-01 12:08:43 +030027 * Creates a cancellation exception with a specified message and [cause].
28 */
29@Suppress("FunctionName")
30public actual fun CancellationException(message: String?, cause: Throwable?) : CancellationException =
31 CancellationException(message.withCause(cause))
32
33/**
Roman Elizarovf29203c2018-01-11 12:39:36 +030034 * 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 Tolstopyatov1f7b2d82018-10-09 15:57:51 +030038internal actual class JobCancellationException public actual constructor(
Roman Elizarovf29203c2018-01-11 12:39:36 +030039 message: String,
40 public override val cause: Throwable?,
Roman Elizarove89cd682018-04-25 13:03:40 +030041 internal actual val job: Job
Roman Elizarovf29203c2018-01-11 12:39:36 +030042) : 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 Elizarovf29203c2018-01-11 12:39:36 +030051@Suppress("FunctionName")
52internal fun IllegalStateException(message: String, cause: Throwable?) =
53 IllegalStateException(message.withCause(cause))
54
Roman Elizarov0aad8f12019-03-01 12:08:43 +030055private fun String?.withCause(cause: Throwable?) =
Roman Elizarov73b456b2019-03-06 17:18:27 +030056 when {
57 cause == null -> this
58 this == null -> "caused by $cause"
59 else -> "$this; caused by $cause"
60 }
Roman Elizarovaa461cf2018-04-11 13:20:29 +030061
62@Suppress("NOTHING_TO_INLINE")
Roman Elizarovd2936392019-03-04 22:20:25 +030063internal actual inline fun Throwable.addSuppressedThrowable(other: Throwable) { /* empty */ }
64
65// For use in tests
66internal actual val RECOVER_STACK_TRACES: Boolean = false