Comick: show error message from API (#3025)

* Comick: show error message from API

* move error handling to an interceptor
This commit is contained in:
Vetle Ledaal 2024-05-16 16:43:40 +00:00 committed by Draff
parent db035c7ad3
commit bf8a3bf3ce
3 changed files with 32 additions and 2 deletions

View File

@ -1,7 +1,7 @@
ext { ext {
extName = 'Comick' extName = 'Comick'
extClass = '.ComickFactory' extClass = '.ComickFactory'
extVersionCode = 43 extVersionCode = 44
isNsfw = true isNsfw = true
} }

View File

@ -21,11 +21,13 @@ import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import okhttp3.Headers import okhttp3.Headers
import okhttp3.HttpUrl.Companion.toHttpUrl import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.Interceptor
import okhttp3.Request import okhttp3.Request
import okhttp3.Response import okhttp3.Response
import rx.Observable import rx.Observable
import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get import uy.kohesive.injekt.api.get
import java.util.concurrent.TimeUnit
import kotlin.math.min import kotlin.math.min
abstract class Comick( abstract class Comick(
@ -155,9 +157,31 @@ abstract class Comick(
} }
override val client = network.client.newBuilder() override val client = network.client.newBuilder()
.rateLimit(3, 1) .addNetworkInterceptor(::errorInterceptor)
.rateLimit(3, 1, TimeUnit.SECONDS)
.build() .build()
private fun errorInterceptor(chain: Interceptor.Chain): Response {
val response = chain.proceed(chain.request())
if (
response.isSuccessful ||
"application/json" !in response.header("Content-Type").orEmpty()
) {
return response
}
val error = try {
response.parseAs<Error>()
} catch (_: Exception) {
null
}
error?.run {
throw Exception("$name error $statusCode: $message")
} ?: throw Exception("HTTP error ${response.code}")
}
/** Popular Manga **/ /** Popular Manga **/
override fun popularMangaRequest(page: Int): Request { override fun popularMangaRequest(page: Int): Request {
val url = "$apiUrl/v1.0/search?sort=follow&limit=$LIMIT&page=$page&tachiyomi=true" val url = "$apiUrl/v1.0/search?sort=follow&limit=$LIMIT&page=$page&tachiyomi=true"

View File

@ -197,3 +197,9 @@ class ChapterPageData(
class Page( class Page(
val url: String? = null, val url: String? = null,
) )
@Serializable
class Error(
val statusCode: Int,
val message: String,
)