Skip to content

Commit 4217a71

Browse files
authored
Updated the safe function for more ergonomic code (#1675)
1 parent 1922055 commit 4217a71

File tree

1 file changed

+41
-2
lines changed

1 file changed

+41
-2
lines changed

library/src/commonMain/kotlin/com/lagradost/cloudstream3/mvvm/ArchComponentExt.kt

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.lagradost.cloudstream3.mvvm
33
import com.lagradost.api.BuildConfig
44
import com.lagradost.api.Log
55
import com.lagradost.cloudstream3.ErrorLoadingException
6+
import com.lagradost.cloudstream3.Prerelease
67
import kotlinx.coroutines.*
78
import java.io.InterruptedIOException
89
import java.net.SocketTimeoutException
@@ -67,6 +68,7 @@ fun logError(throwable: Throwable) {
6768
Log.d("ApiError", "-------------------------------------------------------------------")
6869
}
6970

71+
@Deprecated("Outdated function, use `safe` instead when the new stable is released", ReplaceWith("safe"), level = DeprecationLevel.WARNING)
7072
fun <T> normalSafeApiCall(apiCall: () -> T): T? {
7173
return try {
7274
apiCall.invoke()
@@ -76,6 +78,31 @@ fun <T> normalSafeApiCall(apiCall: () -> T): T? {
7678
}
7779
}
7880

81+
/** Catches any exception (or error) and only logs it.
82+
* Will return null on exceptions. */
83+
@Prerelease
84+
fun <T> safe(apiCall: () -> T): T? {
85+
return try {
86+
apiCall.invoke()
87+
} catch (throwable: Throwable) {
88+
logError(throwable)
89+
return null
90+
}
91+
}
92+
93+
/** Catches any exception (or error) and only logs it.
94+
* Will return null on exceptions. */
95+
@Prerelease
96+
suspend fun <T> safeAsync(apiCall: suspend () -> T): T? {
97+
return try {
98+
apiCall.invoke()
99+
} catch (throwable: Throwable) {
100+
logError(throwable)
101+
return null
102+
}
103+
}
104+
105+
@Deprecated("Outdated function, use `safeAsync` instead when the new stable is released", ReplaceWith("safeAsync"), level = DeprecationLevel.WARNING)
79106
suspend fun <T> suspendSafeApiCall(apiCall: suspend () -> T): T? {
80107
return try {
81108
apiCall.invoke()
@@ -119,7 +146,7 @@ fun CoroutineScope.launchSafe(
119146
return this.launch(context, start, obj)
120147
}
121148

122-
fun<T> throwAbleToResource(
149+
fun <T> throwAbleToResource(
123150
throwable: Throwable
124151
): Resource<T> {
125152
return when (throwable) {
@@ -131,6 +158,7 @@ fun<T> throwAbleToResource(
131158
"App or extension is outdated, update the app or try pre-release.\n${throwable.message}" // todo add exact version?
132159
)
133160
}
161+
134162
is NullPointerException -> {
135163
for (line in throwable.stackTrace) {
136164
if (line?.fileName?.endsWith("provider.kt", ignoreCase = true) == true) {
@@ -144,6 +172,7 @@ fun<T> throwAbleToResource(
144172
}
145173
safeFail(throwable)
146174
}
175+
147176
is SocketTimeoutException, is InterruptedIOException -> {
148177
Resource.Failure(
149178
true,
@@ -161,8 +190,14 @@ fun<T> throwAbleToResource(
161190
// )
162191
// }
163192
is UnknownHostException -> {
164-
Resource.Failure(true, null, null, "Cannot connect to server, try again later.\n${throwable.message}")
193+
Resource.Failure(
194+
true,
195+
null,
196+
null,
197+
"Cannot connect to server, try again later.\n${throwable.message}"
198+
)
165199
}
200+
166201
is ErrorLoadingException -> {
167202
Resource.Failure(
168203
true,
@@ -171,9 +206,11 @@ fun<T> throwAbleToResource(
171206
throwable.message ?: "Error loading, try again later."
172207
)
173208
}
209+
174210
is NotImplementedError -> {
175211
Resource.Failure(false, null, null, "This operation is not implemented.")
176212
}
213+
177214
is SSLHandshakeException -> {
178215
Resource.Failure(
179216
true,
@@ -182,11 +219,13 @@ fun<T> throwAbleToResource(
182219
(throwable.message ?: "SSLHandshakeException") + "\nTry a VPN or DNS."
183220
)
184221
}
222+
185223
is CancellationException -> {
186224
throwable.cause?.let {
187225
throwAbleToResource(it)
188226
} ?: safeFail(throwable)
189227
}
228+
190229
else -> safeFail(throwable)
191230
}
192231
}

0 commit comments

Comments
 (0)