mirror of
https://github.com/Shabinder/SpotiFlyer.git
synced 2024-11-22 17:14:32 +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())
|
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()) {
|
Box(modifier = modifier.fillMaxSize()) {
|
||||||
// TODO Better Null Handling
|
|
||||||
val result = model.queryResult
|
val result = model.queryResult
|
||||||
if (result == null) {
|
if (result == null) {
|
||||||
/* Loading Bar */
|
/* Loading Bar */
|
||||||
@ -69,14 +76,6 @@ fun SpotiFlyerListContent(
|
|||||||
Spacer(modifier.padding(8.dp))
|
Spacer(modifier.padding(8.dp))
|
||||||
Text("Loading..", style = appNameStyle, color = colorPrimary)
|
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 {
|
} else {
|
||||||
LazyColumn(
|
LazyColumn(
|
||||||
verticalArrangement = Arrangement.spacedBy(12.dp),
|
verticalArrangement = Arrangement.spacedBy(12.dp),
|
||||||
|
@ -46,10 +46,14 @@ class GaanaProvider(
|
|||||||
if (type == "Error" || link == "Error") {
|
if (type == "Error" || link == "Error") {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
return gaanaSearch(
|
return try {
|
||||||
type,
|
gaanaSearch(
|
||||||
link
|
type,
|
||||||
)
|
link
|
||||||
|
)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun gaanaSearch(
|
private suspend fun gaanaSearch(
|
||||||
|
@ -100,10 +100,24 @@ class SpotifyProvider(
|
|||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
return spotifySearch(
|
return try {
|
||||||
type,
|
spotifySearch(
|
||||||
link
|
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(
|
private suspend fun spotifySearch(
|
||||||
|
@ -28,11 +28,15 @@ class YoutubeMp3(
|
|||||||
override val logger: Kermit,
|
override val logger: Kermit,
|
||||||
private val dir: Dir,
|
private val dir: Dir,
|
||||||
) : Yt1sMp3 {
|
) : Yt1sMp3 {
|
||||||
suspend fun getMp3DownloadLink(videoID: String): String? = getLinkFromYt1sMp3(videoID)?.let {
|
suspend fun getMp3DownloadLink(videoID: String): String? = try {
|
||||||
logger.i { "Download Link: $it" }
|
getLinkFromYt1sMp3(videoID)?.let {
|
||||||
if (currentPlatform is AllPlatforms.Js/* && corsProxy !is CorsProxy.PublicProxyWithExtension*/)
|
logger.i { "Download Link: $it" }
|
||||||
"https://kind-grasshopper-73.telebit.io/cors/$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
|
// "https://spotiflyer.azurewebsites.net/$it" // Data OUT Limit issue
|
||||||
else it
|
else it
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -47,12 +47,17 @@ class YoutubeMusic constructor(
|
|||||||
private val tag = "YT Music"
|
private val tag = "YT Music"
|
||||||
|
|
||||||
suspend fun getYTIDBestMatch(query: String, trackDetails: TrackDetails): String? {
|
suspend fun getYTIDBestMatch(query: String, trackDetails: TrackDetails): String? {
|
||||||
return sortByBestMatch(
|
return try {
|
||||||
getYTTracks(query),
|
sortByBestMatch(
|
||||||
trackName = trackDetails.title,
|
getYTTracks(query),
|
||||||
trackArtists = trackDetails.artists,
|
trackName = trackDetails.title,
|
||||||
trackDurationSec = trackDetails.durationSec
|
trackArtists = trackDetails.artists,
|
||||||
).keys.firstOrNull()
|
trackDurationSec = trackDetails.durationSec
|
||||||
|
).keys.firstOrNull()
|
||||||
|
} catch (e:Exception) {
|
||||||
|
// All Internet/Client Related Errors
|
||||||
|
null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
private suspend fun getYTTracks(query: String): List<YoutubeTrack> {
|
private suspend fun getYTTracks(query: String): List<YoutubeTrack> {
|
||||||
val youtubeTracks = mutableListOf<YoutubeTrack>()
|
val youtubeTracks = mutableListOf<YoutubeTrack>()
|
||||||
|
@ -28,9 +28,13 @@ import io.ktor.client.request.post
|
|||||||
import io.ktor.http.Parameters
|
import io.ktor.http.Parameters
|
||||||
|
|
||||||
suspend fun authenticateSpotify(): TokenData? {
|
suspend fun authenticateSpotify(): TokenData? {
|
||||||
return if (isInternetAvailable) spotifyAuthClient.post("https://accounts.spotify.com/api/token") {
|
return try {
|
||||||
body = FormDataContent(Parameters.build { append("grant_type", "client_credentials") })
|
if (isInternetAvailable) spotifyAuthClient.post("https://accounts.spotify.com/api/token") {
|
||||||
} else null
|
body = FormDataContent(Parameters.build { append("grant_type", "client_credentials") })
|
||||||
|
} else null
|
||||||
|
}catch (e:Exception) {
|
||||||
|
null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private val spotifyAuthClient by lazy {
|
private val spotifyAuthClient by lazy {
|
||||||
|
Loading…
Reference in New Issue
Block a user