Skip to content

Commit 5667bcf

Browse files
konrad-kaminskielizarov
authored andcommitted
Basic CoroutineExceptionHandler implementation.
1 parent 15d0ec8 commit 5667bcf

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

kotlinx-coroutines-core/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ Select expression to perform multiple suspending operations simultaneously until
100100
[suspendCancellableCoroutine]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/suspend-cancellable-coroutine.html
101101
[NonCancellable]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-non-cancellable/index.html
102102
[newCoroutineContext]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/new-coroutine-context.html
103+
[CoroutineExceptionHandler]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-coroutine-exception-handler/index.html
103104
<!--- INDEX kotlinx.coroutines.experimental.sync -->
104105
[kotlinx.coroutines.experimental.sync.Mutex]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.sync/-mutex/index.html
105106
[kotlinx.coroutines.experimental.sync.Mutex.lock]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.sync/-mutex/lock.html

kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/CoroutineExceptionHandler.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package kotlinx.coroutines.experimental
1818

19+
import kotlin.coroutines.experimental.AbstractCoroutineContextElement
1920
import kotlin.coroutines.experimental.CoroutineContext
2021

2122

@@ -51,7 +52,17 @@ public interface CoroutineExceptionHandler : CoroutineContext.Element {
5152
/**
5253
* Key for [CoroutineExceptionHandler] instance in the coroutine context.
5354
*/
54-
companion object Key : CoroutineContext.Key<CoroutineExceptionHandler>
55+
companion object Key : CoroutineContext.Key<CoroutineExceptionHandler> {
56+
/**
57+
* Creates new [CoroutineExceptionHandler] instance.
58+
* @param handler a function which handles exception thrown by a coroutine
59+
*/
60+
public operator inline fun invoke(crossinline handler: (CoroutineContext, Throwable) -> Unit): CoroutineExceptionHandler =
61+
object: AbstractCoroutineContextElement(Key), CoroutineExceptionHandler {
62+
override fun handleException(context: CoroutineContext, exception: Throwable) =
63+
handler.invoke(context, exception)
64+
}
65+
}
5566

5667
/**
5768
* Handles uncaught [exception] in the given [context]. It is invoked
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package kotlinx.coroutines.experimental
2+
3+
import org.junit.Test
4+
import java.util.concurrent.CountDownLatch
5+
import java.util.concurrent.TimeUnit
6+
7+
class CoroutineExceptionHandlerTest {
8+
@Test
9+
fun testCoroutineExceptionHandlerCreator() {
10+
val latch = CountDownLatch(1)
11+
var coroutineException: Throwable? = null
12+
13+
val handler = CoroutineExceptionHandler { _, ex ->
14+
coroutineException = ex
15+
latch.countDown()
16+
}
17+
18+
launch(CommonPool + handler) {
19+
throw TestException()
20+
}
21+
22+
latch.await(10, TimeUnit.SECONDS)
23+
24+
check(coroutineException is TestException)
25+
}
26+
}
27+
28+
private class TestException: RuntimeException()

0 commit comments

Comments
 (0)