blob: 0d95eba7929d79904e7a5e10d872c783f7fd2234 [file] [log] [blame]
Roman Elizarovf29203c2018-01-11 12:39:36 +03001/*
2 * Copyright 2016-2017 JetBrains s.r.o.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package kotlinx.coroutines.experimental
18
19/**
20 * This exception gets thrown if an exception is caught while processing [CompletionHandler] invocation for [Job].
21 */
22public actual class CompletionHandlerException public actual constructor(
23 message: String,
24 public override val cause: Throwable
25) : RuntimeException(message.withCause(cause))
26
27public actual open class CancellationException actual constructor(message: String) : IllegalStateException(message)
28
29/**
30 * Thrown by cancellable suspending functions if the [Job] of the coroutine is cancelled or completed
31 * without cause, or with a cause or exception that is not [CancellationException]
32 * (see [Job.getCancellationException]).
33 */
34public actual class JobCancellationException public actual constructor(
35 message: String,
36 public override val cause: Throwable?,
37 /**
38 * The job that was cancelled.
39 */
40 public actual val job: Job
41) : CancellationException(message.withCause(cause)) {
42 override fun toString(): String = "${super.toString()}; job=$job"
43 override fun equals(other: Any?): Boolean =
44 other === this ||
45 other is JobCancellationException && other.message == message && other.job == job && other.cause == cause
46 override fun hashCode(): Int =
47 (message!!.hashCode() * 31 + job.hashCode()) * 31 + (cause?.hashCode() ?: 0)
48}
49
50/**
51 * This exception is thrown by [withTimeout] to indicate timeout.
52 */
53@Suppress("DEPRECATION")
54public actual class TimeoutCancellationException internal constructor(
55 message: String,
56 internal val coroutine: Job?
57) : CancellationException(message) {
58 /**
59 * Creates timeout exception with a given message.
60 */
61 public actual constructor(message: String) : this(message, null)
62}
63
64@Suppress("FunctionName")
65internal fun TimeoutCancellationException(
66 time: Int,
67 coroutine: Job
68) : TimeoutCancellationException = TimeoutCancellationException("Timed out waiting for $time", coroutine)
69
70internal actual class DispatchException actual constructor(message: String, cause: Throwable) : RuntimeException(message.withCause(cause))
71
72@Suppress("FunctionName")
73internal fun IllegalStateException(message: String, cause: Throwable?) =
74 IllegalStateException(message.withCause(cause))
75
76private fun String.withCause(cause: Throwable?) =
77 if (cause == null) this else "$this; caused by $cause"