diff --git a/app/src/main/java/com/xiaojinzi/support/demo/App.kt b/app/src/main/java/com/xiaojinzi/support/demo/App.kt index 986c624..c7f2774 100644 --- a/app/src/main/java/com/xiaojinzi/support/demo/App.kt +++ b/app/src/main/java/com/xiaojinzi/support/demo/App.kt @@ -17,6 +17,7 @@ import com.xiaojinzi.support.ktx.ComponentLifecycleCallback import com.xiaojinzi.support.ktx.LogSupport import com.xiaojinzi.support.ktx.MemoryCache import com.xiaojinzi.support.ktx.MemoryCacheConfig +import com.xiaojinzi.support.ktx.findException import com.xiaojinzi.support.logger.AndroidLogAdapter import com.xiaojinzi.support.logger.Logger import kotlinx.coroutines.delay diff --git a/lib-ktx/src/main/java/com/xiaojinzi/support/ktx/Exceptions.kt b/lib-ktx/src/main/java/com/xiaojinzi/support/ktx/Exceptions.kt index fe67769..983d230 100644 --- a/lib-ktx/src/main/java/com/xiaojinzi/support/ktx/Exceptions.kt +++ b/lib-ktx/src/main/java/com/xiaojinzi/support/ktx/Exceptions.kt @@ -1,5 +1,7 @@ package com.xiaojinzi.support.ktx +import kotlin.reflect.KClass + /** * 获取真实的错误 */ @@ -10,4 +12,19 @@ val Throwable.realThrowable: Throwable throwable = throwable.cause!! } return throwable - } \ No newline at end of file + } + +/** + * 会从顶端开始找是否是 [targetClass] 类型的异常, 找到就返回 + */ +@Suppress("UNCHECKED_CAST") +fun Throwable.findException(targetClass: KClass): T? { + var throwable: Throwable? = this + while (throwable != null) { + if (targetClass.isInstance(throwable)) { + return throwable as T + } + throwable = throwable.cause + } + return null +} \ No newline at end of file diff --git a/lib-ktx/src/main/java/com/xiaojinzi/support/ktx/Files.kt b/lib-ktx/src/main/java/com/xiaojinzi/support/ktx/Files.kt index 8d73569..e1b7206 100644 --- a/lib-ktx/src/main/java/com/xiaojinzi/support/ktx/Files.kt +++ b/lib-ktx/src/main/java/com/xiaojinzi/support/ktx/Files.kt @@ -8,6 +8,7 @@ import java.io.File */ suspend fun File.deleteAfterUse( isDeleteAfterSuccess: Boolean = true, + // 这里这个应该默认也要是 true, 我的感觉是这样的 isDeleteAfterError: Boolean = false, action: suspend (File) -> T ): T {