Error Handling / try-catch

This commit is contained in:
shabinder 2021-04-29 16:19:50 +05:30
parent 318c673cea
commit 815eb9ca99
6 changed files with 62 additions and 32 deletions

View File

@ -59,8 +59,15 @@ fun SpotiFlyerListContent(
) {
val model by component.models.collectAsState(SpotiFlyerList.State())
LaunchedEffect(model.errorOccurred) {
/*Handle if Any Exception Occurred*/
model.errorOccurred?.let {
showPopUpMessage(it.message ?: "An Error Occurred, Check your Link / Connection")
component.onBackPressed()
}
}
Box(modifier = modifier.fillMaxSize()) {
// TODO Better Null Handling
val result = model.queryResult
if (result == null) {
/* Loading Bar */
@ -69,14 +76,6 @@ fun SpotiFlyerListContent(
Spacer(modifier.padding(8.dp))
Text("Loading..", style = appNameStyle, color = colorPrimary)
}
LaunchedEffect(Unit) {
delay(350)
/*Handle if Any Exception Occurred*/
model.errorOccurred?.let {
showPopUpMessage(it.message ?: "An Error Occurred, Check your Link / Connection")
component.onBackPressed()
}
}
} else {
LazyColumn(
verticalArrangement = Arrangement.spacedBy(12.dp),

View File

@ -46,10 +46,14 @@ class GaanaProvider(
if (type == "Error" || link == "Error") {
return null
}
return gaanaSearch(
type,
link
)
return try {
gaanaSearch(
type,
link
)
} catch (e: Exception) {
null
}
}
private suspend fun gaanaSearch(

View File

@ -100,10 +100,24 @@ class SpotifyProvider(
return null
}
return spotifySearch(
type,
link
)
return try {
spotifySearch(
type,
link
)
}catch (e: Exception){
// Try Reinitialising Client // Handle 401 Token Expiry ,etc Exceptions
authenticateSpotifyClient(true)
// Retry Search
try {
spotifySearch(
type,
link
)
} catch (e:Exception){
null
}
}
}
private suspend fun spotifySearch(

View File

@ -28,11 +28,15 @@ class YoutubeMp3(
override val logger: Kermit,
private val dir: Dir,
) : Yt1sMp3 {
suspend fun getMp3DownloadLink(videoID: String): String? = getLinkFromYt1sMp3(videoID)?.let {
logger.i { "Download Link: $it" }
if (currentPlatform is AllPlatforms.Js/* && corsProxy !is CorsProxy.PublicProxyWithExtension*/)
"https://kind-grasshopper-73.telebit.io/cors/$it"
suspend fun getMp3DownloadLink(videoID: String): String? = try {
getLinkFromYt1sMp3(videoID)?.let {
logger.i { "Download Link: $it" }
if (currentPlatform is AllPlatforms.Js/* && corsProxy !is CorsProxy.PublicProxyWithExtension*/)
"https://kind-grasshopper-73.telebit.io/cors/$it"
// "https://spotiflyer.azurewebsites.net/$it" // Data OUT Limit issue
else it
else it
}
} catch (e: Exception) {
null
}
}

View File

@ -47,12 +47,17 @@ class YoutubeMusic constructor(
private val tag = "YT Music"
suspend fun getYTIDBestMatch(query: String, trackDetails: TrackDetails): String? {
return sortByBestMatch(
getYTTracks(query),
trackName = trackDetails.title,
trackArtists = trackDetails.artists,
trackDurationSec = trackDetails.durationSec
).keys.firstOrNull()
return try {
sortByBestMatch(
getYTTracks(query),
trackName = trackDetails.title,
trackArtists = trackDetails.artists,
trackDurationSec = trackDetails.durationSec
).keys.firstOrNull()
} catch (e:Exception) {
// All Internet/Client Related Errors
null
}
}
private suspend fun getYTTracks(query: String): List<YoutubeTrack> {
val youtubeTracks = mutableListOf<YoutubeTrack>()

View File

@ -28,9 +28,13 @@ import io.ktor.client.request.post
import io.ktor.http.Parameters
suspend fun authenticateSpotify(): TokenData? {
return if (isInternetAvailable) spotifyAuthClient.post("https://accounts.spotify.com/api/token") {
body = FormDataContent(Parameters.build { append("grant_type", "client_credentials") })
} else null
return try {
if (isInternetAvailable) spotifyAuthClient.post("https://accounts.spotify.com/api/token") {
body = FormDataContent(Parameters.build { append("grant_type", "client_credentials") })
} else null
}catch (e:Exception) {
null
}
}
private val spotifyAuthClient by lazy {