From 4d983830dfba6c46a065f8f40b2763ab6e22ad7d Mon Sep 17 00:00:00 2001 From: FooIbar <118464521+FooIbar@users.noreply.github.com> Date: Sat, 13 Jan 2024 11:58:53 +0800 Subject: [PATCH] Refine exception translation --- .../com/hippo/ehviewer/client/EhEngine.kt | 6 ++++++ .../ehviewer/client/parser/FavoritesParser.kt | 2 -- .../ehviewer/client/parser/GalleryListParser.kt | 17 ++--------------- app/src/main/rust/src/parser/list.rs | 3 --- 4 files changed, 8 insertions(+), 20 deletions(-) diff --git a/app/src/main/kotlin/com/hippo/ehviewer/client/EhEngine.kt b/app/src/main/kotlin/com/hippo/ehviewer/client/EhEngine.kt index 9783daefd..a0155cec4 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/client/EhEngine.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/client/EhEngine.kt @@ -131,6 +131,12 @@ fun rethrowExactly(code: Int, body: Either, e: Throwable): N if (e is ParseException || e is SerializationException) { body.onLeft { if ("<" !in it) throw EhException(it) } + when (val message = e.cause?.message) { + "No hits found!" -> throw EhException(R.string.gallery_list_empty_hit) + "No watched tags!" -> throw EhException(R.string.gallery_list_empty_hit_subscription) + "Not logged in!" -> throw NotLoggedInException() + is String -> if (message.startsWith("Your IP address")) throw EhException(message) + } if (Settings.saveParseErrorBody) body.saveParseError(e) throw EhException(appCtx.getString(R.string.error_parse_error), e) } diff --git a/app/src/main/kotlin/com/hippo/ehviewer/client/parser/FavoritesParser.kt b/app/src/main/kotlin/com/hippo/ehviewer/client/parser/FavoritesParser.kt index 3da9a1b11..9db1c36b3 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/client/parser/FavoritesParser.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/client/parser/FavoritesParser.kt @@ -1,6 +1,5 @@ package com.hippo.ehviewer.client.parser -import com.hippo.ehviewer.client.exception.NotLoggedInException import com.hippo.ehviewer.client.exception.ParseException import java.nio.ByteBuffer import kotlinx.serialization.Serializable @@ -22,7 +21,6 @@ object FavoritesParser { return runSuspendCatching { unmarshalParsingAs(body, ::parseFav) }.getOrElse { - if (it is RuntimeException && it.message == "Not logged in!") throw NotLoggedInException() throw ParseException("Parse favorites error", it) } } diff --git a/app/src/main/kotlin/com/hippo/ehviewer/client/parser/GalleryListParser.kt b/app/src/main/kotlin/com/hippo/ehviewer/client/parser/GalleryListParser.kt index 1ad94811c..a52dbd2a9 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/client/parser/GalleryListParser.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/client/parser/GalleryListParser.kt @@ -1,11 +1,9 @@ package com.hippo.ehviewer.client.parser import com.hippo.ehviewer.EhDB -import com.hippo.ehviewer.R import com.hippo.ehviewer.client.data.BaseGalleryInfo import com.hippo.ehviewer.client.data.GalleryInfo.Companion.LOCAL_FAVORITED import com.hippo.ehviewer.client.data.GalleryInfo.Companion.NOT_FAVORITED -import com.hippo.ehviewer.client.exception.EhException import com.hippo.ehviewer.client.exception.ParseException import java.nio.ByteBuffer import kotlinx.serialization.Serializable @@ -14,20 +12,9 @@ import moe.tarsin.coroutines.runSuspendCatching object GalleryListParser { suspend fun parse(body: ByteBuffer) = runSuspendCatching { unmarshalParsingAs(body, ::parseGalleryInfoList) - }.onFailure { - if (it is RuntimeException) { - if (it.message?.startsWith("Your IP address") == true) { - throw EhException(it.message) - } - if (it.message == "No watched tags!") { - throw EhException(R.string.gallery_list_empty_hit_subscription) - } - if (it.message == "No hits found!") { - throw EhException(R.string.gallery_list_empty_hit) - } - } + }.getOrElse { throw ParseException("Can't parse gallery list", it) - }.getOrThrow().apply { + }.apply { galleryInfoList.onEach { if (it.favoriteSlot == NOT_FAVORITED && EhDB.containLocalFavorites(it.gid)) { it.favoriteSlot = LOCAL_FAVORITED diff --git a/app/src/main/rust/src/parser/list.rs b/app/src/main/rust/src/parser/list.rs index 4fc76c440..7c45ed915 100644 --- a/app/src/main/rust/src/parser/list.rs +++ b/app/src/main/rust/src/parser/list.rs @@ -194,9 +194,6 @@ pub fn parse_info_list( parser: &Parser, str: &str, ) -> Result { - if !str.contains('<') { - return Err("No content!"); - } if str.contains("

You do not have any watched tags") { return Err("No watched tags!"); }