<lambda>null1 package kotlinx.coroutines.internal
2 
3 import kotlinx.coroutines.*
4 import kotlin.coroutines.*
5 
6 internal typealias OnUndeliveredElement<E> = (E) -> Unit
7 
8 internal fun <E> OnUndeliveredElement<E>.callUndeliveredElementCatchingException(
9     element: E,
10     undeliveredElementException: UndeliveredElementException? = null
11 ): UndeliveredElementException? {
12     try {
13         invoke(element)
14     } catch (ex: Throwable) {
15         // undeliveredElementException.cause !== ex is an optimization in case the same exception is thrown
16         // over and over again by on OnUndeliveredElement
17         if (undeliveredElementException != null && undeliveredElementException.cause !== ex) {
18             undeliveredElementException.addSuppressed(ex)
19         } else {
20             return UndeliveredElementException("Exception in undelivered element handler for $element", ex)
21         }
22     }
23     return undeliveredElementException
24 }
25 
callUndeliveredElementnull26 internal fun <E> OnUndeliveredElement<E>.callUndeliveredElement(element: E, context: CoroutineContext) {
27     callUndeliveredElementCatchingException(element, null)?.let { ex ->
28         handleCoroutineException(context, ex)
29     }
30 }
31 
bindCancellationFunnull32 internal fun <E> OnUndeliveredElement<E>.bindCancellationFun(element: E, context: CoroutineContext): (Throwable) -> Unit =
33     { _: Throwable -> callUndeliveredElement(element, context) }
34 
35 /**
36  * Internal exception that is thrown when [OnUndeliveredElement] handler in
37  * a [kotlinx.coroutines.channels.Channel] throws an exception.
38  */
39 internal class UndeliveredElementException(message: String, cause: Throwable) : RuntimeException(message, cause)
40