Skip to content

Commit

Permalink
Merge pull request #6541 from Bnyro/master
Browse files Browse the repository at this point in the history
refactor: error handling for stream extractor responses
  • Loading branch information
Bnyro authored Sep 26, 2024
2 parents 8c3bb5b + 97708e2 commit 3101d69
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 26 deletions.
16 changes: 16 additions & 0 deletions app/src/main/java/com/github/libretube/api/StreamsExtractor.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.github.libretube.api

import android.content.Context
import com.github.libretube.R
import com.github.libretube.api.obj.ChapterSegment
import com.github.libretube.api.obj.Message
import com.github.libretube.api.obj.MetaInfo
import com.github.libretube.api.obj.PipedStream
import com.github.libretube.api.obj.PreviewFrames
Expand All @@ -14,6 +17,9 @@ import org.schabi.newpipe.extractor.NewPipe
import org.schabi.newpipe.extractor.stream.StreamInfo
import org.schabi.newpipe.extractor.stream.StreamInfoItem
import org.schabi.newpipe.extractor.stream.VideoStream
import retrofit2.HttpException
import java.io.IOException
import java.lang.Exception

fun VideoStream.toPipedStream(): PipedStream = PipedStream(
url = content,
Expand Down Expand Up @@ -150,4 +156,14 @@ object StreamsExtractor {
}
)
}

fun getExtractorErrorMessageString(context: Context, exception: Exception): String {
return when (exception) {
is IOException -> context.getString(R.string.unknown_error)
is HttpException -> exception.response()?.errorBody()?.string()?.runCatching {
JsonHelper.json.decodeFromString<Message>(this).message
}?.getOrNull() ?: context.getString(R.string.server_error)
else -> exception.localizedMessage.orEmpty()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import androidx.fragment.app.DialogFragment
import androidx.fragment.app.setFragmentResult
import androidx.lifecycle.lifecycleScope
import com.github.libretube.R
import com.github.libretube.api.RetrofitInstance
import com.github.libretube.api.StreamsExtractor
import com.github.libretube.api.obj.PipedStream
import com.github.libretube.api.obj.Streams
Expand All @@ -22,6 +21,7 @@ import com.github.libretube.constants.IntentData
import com.github.libretube.databinding.DialogDownloadBinding
import com.github.libretube.extensions.TAG
import com.github.libretube.extensions.getWhileDigit
import com.github.libretube.extensions.toastFromMainDispatcher
import com.github.libretube.helpers.DownloadHelper
import com.github.libretube.helpers.PreferenceHelper
import com.github.libretube.parcelable.DownloadData
Expand All @@ -30,8 +30,6 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import retrofit2.HttpException
import java.io.IOException

class DownloadDialog : DialogFragment() {
private lateinit var videoId: String
Expand Down Expand Up @@ -84,17 +82,11 @@ class DownloadDialog : DialogFragment() {
withContext(Dispatchers.IO) {
StreamsExtractor.extractStreams(videoId)
}
} catch (e: IOException) {
Log.e(TAG(), e.stackTraceToString())
Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show()
return@launch
} catch (e: HttpException) {
Log.e(TAG(), e.stackTraceToString())
Toast.makeText(context, R.string.server_error, Toast.LENGTH_SHORT).show()
return@launch
} catch (e: Exception) {
Log.e(TAG(), e.stackTraceToString())
Toast.makeText(context, e.localizedMessage, Toast.LENGTH_SHORT).show()
val context = context ?: return@launch
val errorMessage = StreamsExtractor.getExtractorErrorMessageString(context, e)
context.toastFromMainDispatcher(errorMessage)
return@launch
}
initDownloadOptions(binding, response)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import com.github.libretube.util.deArrow
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlinx.serialization.encodeToString
import retrofit2.HttpException
import java.io.IOException

@UnstableApi
class PlayerViewModel(
Expand Down Expand Up @@ -53,20 +51,11 @@ class PlayerViewModel(
withContext(Dispatchers.IO) {
if (isOrientationChangeInProgress && streamsInfo != null) return@withContext streamsInfo to null

streamsInfo = try {
StreamsExtractor.extractStreams(videoId).deArrow(videoId)
} catch (e: IOException) {
return@withContext null to context.getString(R.string.unknown_error)
} catch (e: HttpException) {
val errorMessage = e.response()?.errorBody()?.string()?.runCatching {
JsonHelper.json.decodeFromString<Message>(this).message
}?.getOrNull() ?: context.getString(R.string.server_error)
return@withContext null to errorMessage
return@withContext try {
StreamsExtractor.extractStreams(videoId).deArrow(videoId) to null
} catch (e: Exception) {
return@withContext null to e.message
return@withContext null to StreamsExtractor.getExtractorErrorMessageString(context, e)
}

return@withContext streamsInfo to null
}

suspend fun fetchSponsorBlockSegments(videoId: String) = withContext(Dispatchers.IO) {
Expand Down

0 comments on commit 3101d69

Please sign in to comment.