[Remanga] Fix errors handling

pull/6/head
Koitharu 4 years ago
parent 7c7b98fd0d
commit fd4b902b8d
No known key found for this signature in database
GPG Key ID: 8E861F8CE6E7CE27

@ -91,8 +91,11 @@ abstract class MangaLoaderContext {
abstract fun getConfig(source: MangaSource): MangaSourceConfig abstract fun getConfig(source: MangaSource): MangaSourceConfig
private fun Response.ensureSuccess() = apply { private fun Response.ensureSuccess() = apply {
if (!isSuccessful) { val exception: Exception? = when (code) { // Catch some error codes, not all
val exception = HttpStatusException(message, code, request.url.toString()) in 500..599 -> HttpStatusException(message, code, request.url.toString())
else -> null
}
if (exception != null) {
runCatching { runCatching {
close() close()
}.onFailure { }.onFailure {

@ -5,7 +5,6 @@ import okhttp3.Response
import org.json.JSONArray import org.json.JSONArray
import org.json.JSONException import org.json.JSONException
import org.json.JSONObject import org.json.JSONObject
import org.jsoup.HttpStatusException
import org.koitharu.kotatsu.parsers.MangaLoaderContext import org.koitharu.kotatsu.parsers.MangaLoaderContext
import org.koitharu.kotatsu.parsers.MangaParser import org.koitharu.kotatsu.parsers.MangaParser
import org.koitharu.kotatsu.parsers.MangaParserAuthProvider import org.koitharu.kotatsu.parsers.MangaParserAuthProvider
@ -113,12 +112,10 @@ internal class RemangaParser(
val domain = getDomain() val domain = getDomain()
val slug = manga.url.find(regexLastUrlPath) val slug = manga.url.find(regexLastUrlPath)
?: throw ParseException("Cannot obtain slug from ${manga.url}") ?: throw ParseException("Cannot obtain slug from ${manga.url}")
val data = catch401 { val data = context.httpGet(
context.httpGet( url = "https://api.$domain/api/titles/$slug/",
url = "https://api.$domain/api/titles/$slug/", headers = getApiHeaders(),
headers = getApiHeaders(), ).handle401().parseJson()
)
}.parseJson()
val content = try { val content = try {
data.getJSONObject("content") data.getJSONObject("content")
} catch (e: JSONException) { } catch (e: JSONException) {
@ -172,9 +169,9 @@ internal class RemangaParser(
override suspend fun getPages(chapter: MangaChapter): List<MangaPage> { override suspend fun getPages(chapter: MangaChapter): List<MangaPage> {
val referer = "https://${getDomain()}/" val referer = "https://${getDomain()}/"
val content = catch401 { val content = context.httpGet(chapter.url.withDomain(subdomain = "api"), getApiHeaders())
context.httpGet(chapter.url.withDomain(subdomain = "api"), getApiHeaders()) .handle401()
}.parseJson() .parseJson()
.getJSONObject("content") .getJSONObject("content")
val pages = content.optJSONArray("pages") val pages = content.optJSONArray("pages")
if (pages == null) { if (pages == null) {
@ -213,12 +210,10 @@ internal class RemangaParser(
} }
override suspend fun getUsername(): String { override suspend fun getUsername(): String {
val jo = catch401 { val jo = context.httpGet(
context.httpGet( url = "https://api.${getDomain()}/api/users/current/",
url = "https://api.${getDomain()}/api/users/current/", headers = getApiHeaders(),
headers = getApiHeaders(), ).handle401().parseJson()
)
}.parseJson()
return jo.getJSONObject("content").getString("username") return jo.getJSONObject("content").getString("username")
} }
@ -256,12 +251,10 @@ internal class RemangaParser(
val result = ArrayList<JSONObject>(100) val result = ArrayList<JSONObject>(100)
var page = 1 var page = 1
while (true) { while (true) {
val content = catch401 { val content = context.httpGet(
context.httpGet( url = "https://api.$domain/api/titles/chapters/?branch_id=$branchId&page=$page&count=100",
url = "https://api.$domain/api/titles/chapters/?branch_id=$branchId&page=$page&count=100", headers = getApiHeaders(),
headers = getApiHeaders(), ).handle401().parseJson().getJSONArray("content")
)
}.parseJson().getJSONArray("content")
val len = content.length() val len = content.length()
if (len == 0) { if (len == 0) {
break break
@ -275,13 +268,9 @@ internal class RemangaParser(
return result return result
} }
private inline fun catch401(block: () -> Response): Response = try { private fun Response.handle401() = apply {
block() if (code == HttpURLConnection.HTTP_UNAUTHORIZED) {
} catch (e: HttpStatusException) {
if (e.statusCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
throw AuthRequiredException(source) throw AuthRequiredException(source)
} else {
throw e
} }
} }
} }

@ -135,7 +135,7 @@ internal class MangaParserTest {
for (item in list) { for (item in list) {
assert(item.url.isNotEmpty()) assert(item.url.isNotEmpty())
assert(!item.url.isUrlAbsoulte()) assert(!item.url.isUrlAbsoulte())
assert(item.coverUrl.isUrlAbsoulte()) assert(item.coverUrl.isUrlAbsoulte()) { "Cover url is not absolute: ${item.coverUrl}" }
assert(item.title.isNotEmpty()) { "Title for ${item.publicUrl} is empty" } assert(item.title.isNotEmpty()) { "Title for ${item.publicUrl} is empty" }
assert(item.publicUrl.isUrlAbsoulte()) assert(item.publicUrl.isUrlAbsoulte())
} }

Loading…
Cancel
Save