update documentation

This commit is contained in:
InsanusMokrassar 2020-12-22 15:21:25 +06:00
parent 069e51f2ff
commit 14aa9ca26c

View File

@ -12,8 +12,25 @@ typealias ExceptionHandler<T> = suspend (Throwable) -> T
*/ */
var defaultSafelyExceptionHandler: ExceptionHandler<Nothing> = { throw it } var defaultSafelyExceptionHandler: ExceptionHandler<Nothing> = { throw it }
/**
* Key for [SafelyExceptionHandler] which can be used in [CoroutineContext.get] to get current default
* [SafelyExceptionHandler]
*/
class SafelyExceptionHandlerKey<T> : CoroutineContext.Key<SafelyExceptionHandler<T>> class SafelyExceptionHandlerKey<T> : CoroutineContext.Key<SafelyExceptionHandler<T>>
/**
* Shortcut for creating instance of [SafelyExceptionHandlerKey]
*/
@Suppress("NOTHING_TO_INLINE")
inline fun <T> safelyExceptionHandlerKey() = SafelyExceptionHandlerKey<T>() inline fun <T> safelyExceptionHandlerKey() = SafelyExceptionHandlerKey<T>()
/**
* Wrapper for [ExceptionHandler] which can be used in [CoroutineContext] to set local (for [CoroutineContext]) default
* [ExceptionHandler]. To get it use [CoroutineContext.get] with key [SafelyExceptionHandlerKey]
*
* @see SafelyExceptionHandlerKey
* @see ExceptionHandler
*/
class SafelyExceptionHandler<T>( class SafelyExceptionHandler<T>(
val handler: ExceptionHandler<T> val handler: ExceptionHandler<T>
) : CoroutineContext.Element { ) : CoroutineContext.Element {
@ -24,11 +41,19 @@ class SafelyExceptionHandler<T>(
/** /**
* It will run [block] inside of [supervisorScope] to avoid problems with catching of exceptions * It will run [block] inside of [supervisorScope] to avoid problems with catching of exceptions
* *
* Priorities of [ExceptionHandler]s:
*
* * [onException] In case if custom (will be used anyway if not [defaultSafelyExceptionHandler])
* * [CoroutineContext.get] with [SafelyExceptionHandlerKey] as key
* * [defaultSafelyExceptionHandler]
*
* @param [onException] Will be called when happen exception inside of [block]. By default will throw exception - this * @param [onException] Will be called when happen exception inside of [block]. By default will throw exception - this
* exception will be available for catching * exception will be available for catching
* *
* @see defaultSafelyExceptionHandler * @see defaultSafelyExceptionHandler
* @see safelyWithoutExceptions * @see safelyWithoutExceptions
* @see SafelyExceptionHandlerKey
* @see SafelyExceptionHandler
*/ */
suspend inline fun <T> safely( suspend inline fun <T> safely(
noinline onException: ExceptionHandler<T> = defaultSafelyExceptionHandler, noinline onException: ExceptionHandler<T> = defaultSafelyExceptionHandler,