Better error messages when a video is unavailable

This commit is contained in:
Bnyro 2023-02-18 10:29:26 +01:00
parent c673b7464b
commit da04059359
2 changed files with 12 additions and 6 deletions

View File

@ -3,4 +3,7 @@ package com.github.libretube.api.obj
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
@Serializable @Serializable
data class Message(val message: String? = null) data class Message(
val error: String? = null,
val message: String? = null
)

View File

@ -44,10 +44,12 @@ import com.github.libretube.api.CronetHelper
import com.github.libretube.api.JsonHelper import com.github.libretube.api.JsonHelper
import com.github.libretube.api.RetrofitInstance import com.github.libretube.api.RetrofitInstance
import com.github.libretube.api.obj.ChapterSegment import com.github.libretube.api.obj.ChapterSegment
import com.github.libretube.api.obj.Message
import com.github.libretube.api.obj.PipedStream import com.github.libretube.api.obj.PipedStream
import com.github.libretube.api.obj.Segment import com.github.libretube.api.obj.Segment
import com.github.libretube.api.obj.StreamItem import com.github.libretube.api.obj.StreamItem
import com.github.libretube.api.obj.Streams import com.github.libretube.api.obj.Streams
import com.github.libretube.api.obj.Token
import com.github.libretube.constants.IntentData import com.github.libretube.constants.IntentData
import com.github.libretube.constants.PreferenceKeys import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.databinding.FragmentPlayerBinding import com.github.libretube.databinding.FragmentPlayerBinding
@ -118,6 +120,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import kotlinx.datetime.LocalDate import kotlinx.datetime.LocalDate
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString import kotlinx.serialization.encodeToString
import retrofit2.HttpException import retrofit2.HttpException
@ -653,13 +656,13 @@ class PlayerFragment : Fragment(R.layout.fragment_player), OnlinePlayerOptions {
streams = try { streams = try {
RetrofitInstance.api.getStreams(videoId!!) RetrofitInstance.api.getStreams(videoId!!)
} catch (e: IOException) { } catch (e: IOException) {
println(e) Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_LONG).show()
Log.e(TAG(), "IOException, you might not have internet connection")
Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show()
return@launchWhenCreated return@launchWhenCreated
} catch (e: HttpException) { } catch (e: HttpException) {
Log.e(TAG(), "HttpException, unexpected response") val errorMessage = e.response()?.errorBody()?.string()?.let {
Toast.makeText(context, R.string.server_error, Toast.LENGTH_SHORT).show() JsonHelper.json.decodeFromString<Message>(it).message
} ?: context?.getString(R.string.server_error) ?: ""
Toast.makeText(context, errorMessage, Toast.LENGTH_LONG).show()
return@launchWhenCreated return@launchWhenCreated
} }