mirror of
https://github.com/Shabinder/SpotiFlyer.git
synced 2024-12-22 20:57:54 +01:00
Error Handling / try-catch
This commit is contained in:
parent
318c673cea
commit
815eb9ca99
@ -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),
|
||||
|
@ -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(
|
||||
|
@ -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(
|
||||
|
@ -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"
|
||||
// "https://spotiflyer.azurewebsites.net/$it" // Data OUT Limit issue
|
||||
else 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
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
@ -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>()
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user