Add extension functions that accept reified types.

This commit is contained in:
Isira Seneviratne 2020-12-15 16:45:34 +05:30
parent 486e720e00
commit 50dcf308a2
2 changed files with 51 additions and 41 deletions

View file

@ -15,18 +15,28 @@ val Throwable.isInterruptedCaused: Boolean
* @return if throwable is related to network issues, or one of its causes is.
*/
val Throwable.isNetworkRelated: Boolean
get() = hasAssignableCause(IOException::class.java)
get() = hasAssignableCause<IOException>()
/**
* Calls [hasCause] with the `checkSubtypes` parameter set to false.
*/
fun Throwable.hasExactCause(vararg causesToCheck: Class<*>) = hasCause(false, *causesToCheck)
/**
* Calls [hasCause] with a reified [Throwable] type.
*/
inline fun <reified T : Throwable> Throwable.hasExactCause() = hasExactCause(T::class.java)
/**
* Calls [hasCause] with the `checkSubtypes` parameter set to true.
*/
fun Throwable?.hasAssignableCause(vararg causesToCheck: Class<*>) = hasCause(true, *causesToCheck)
/**
* Calls [hasCause] with a reified [Throwable] type.
*/
inline fun <reified T : Throwable> Throwable?.hasAssignableCause() = hasAssignableCause(T::class.java)
/**
* Check if the throwable has some cause from the causes to check, or is itself in it.
*