From c7c0446f2365482a4f605bfad727d64f59b4ba20 Mon Sep 17 00:00:00 2001 From: FooIbar <118464521+FooIbar@users.noreply.github.com> Date: Fri, 8 Sep 2023 14:49:28 +0800 Subject: [PATCH] Tweak ParseException --- .../main/java/com/hippo/ehviewer/client/EhEngine.kt | 4 ++-- .../ehviewer/client/exception/ParseException.kt | 13 ++----------- .../hippo/ehviewer/client/parser/FavoritesParser.kt | 2 +- .../ehviewer/client/parser/GalleryDetailParser.kt | 10 +++++----- .../ehviewer/client/parser/GalleryListParser.kt | 2 +- .../parser/GalleryMultiPageViewerPTokenParser.kt | 2 +- .../ehviewer/client/parser/GalleryPageApiParser.kt | 4 ++-- .../ehviewer/client/parser/GalleryPageParser.kt | 2 +- .../com/hippo/ehviewer/client/parser/HomeParser.kt | 2 +- .../hippo/ehviewer/client/parser/ProfileParser.kt | 2 +- .../hippo/ehviewer/client/parser/SignInParser.kt | 2 +- .../hippo/ehviewer/client/parser/TorrentParser.kt | 5 ++++- .../main/java/com/hippo/ehviewer/util/AppConfig.kt | 4 ++-- app/src/main/rust/src/parser/torrent.rs | 2 +- 14 files changed, 25 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/com/hippo/ehviewer/client/EhEngine.kt b/app/src/main/java/com/hippo/ehviewer/client/EhEngine.kt index 9d80f269f..d6442efa7 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/EhEngine.kt +++ b/app/src/main/java/com/hippo/ehviewer/client/EhEngine.kt @@ -120,8 +120,8 @@ private fun rethrowExactly(code: Int, headers: Headers, body: String, e: Throwab } else if ("<" !in body) { throw EhException(body) } else { - if (Settings.saveParseErrorBody) AppConfig.saveParseErrorBody(e) - throw EhException(appCtx.getString(R.string.error_parse_error)) + if (Settings.saveParseErrorBody) AppConfig.saveParseErrorBody(e, body) + throw EhException(appCtx.getString(R.string.error_parse_error), e) } } diff --git a/app/src/main/java/com/hippo/ehviewer/client/exception/ParseException.kt b/app/src/main/java/com/hippo/ehviewer/client/exception/ParseException.kt index df59bcce7..101d1ed54 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/exception/ParseException.kt +++ b/app/src/main/java/com/hippo/ehviewer/client/exception/ParseException.kt @@ -16,16 +16,7 @@ package com.hippo.ehviewer.client.exception class ParseException : EhException { - val body: String + constructor(detailMessage: String?) : super(detailMessage) - constructor(detailMessage: String?, body: String) : super(detailMessage) { - this.body = body - } - - constructor(detailMessage: String?, body: String, cause: Throwable?) : super( - detailMessage, - cause, - ) { - this.body = body - } + constructor(detailMessage: String?, cause: Throwable?) : super(detailMessage, cause) } diff --git a/app/src/main/java/com/hippo/ehviewer/client/parser/FavoritesParser.kt b/app/src/main/java/com/hippo/ehviewer/client/parser/FavoritesParser.kt index 1eee9ec63..0ac3aecb9 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/parser/FavoritesParser.kt +++ b/app/src/main/java/com/hippo/ehviewer/client/parser/FavoritesParser.kt @@ -12,7 +12,7 @@ object FavoritesParser { } val catArray = arrayOfNulls(10) val countArray = parseFav(body, catArray) - if (countArray.isEmpty()) throw ParseException("Parse favorites error", body) + if (countArray.isEmpty()) throw ParseException("Parse favorites error") val result = GalleryListParser.parse(body) return Result(catArray.requireNoNulls(), countArray, result) } diff --git a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryDetailParser.kt b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryDetailParser.kt index 366c5dd4d..648d89872 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryDetailParser.kt +++ b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryDetailParser.kt @@ -114,9 +114,9 @@ object GalleryDetailParser { gd.token = groupValues[2] gd.apiUid = groupValues[3].toLongOrNull() ?: -1L gd.apiKey = groupValues[4] - } ?: throw ParseException("Can't parse gallery detail", body) + } ?: throw ParseException("Can't parse gallery detail") if (gd.gid == -1L) { - throw ParseException("Can't parse gallery detail", body) + throw ParseException("Can't parse gallery detail") } PATTERN_TORRENT.find(body)?.run { gd.torrentUrl = groupValues[1].trim().unescapeXml() @@ -214,7 +214,7 @@ object GalleryDetailParser { } } catch (e: Throwable) { ExceptionUtils.throwIfFatal(e) - throw ParseException("Can't parse gallery detail", body) + throw ParseException("Can't parse gallery detail") } // newer version @@ -450,7 +450,7 @@ object GalleryDetailParser { */ fun parsePreviewPages(body: String): Int { return PATTERN_PREVIEW_PAGES.find(body)?.groupValues?.get(1)?.toIntOrNull() - ?: throw ParseException("Parse preview page count error", body) + ?: throw ParseException("Parse preview page count error") } /** @@ -458,7 +458,7 @@ object GalleryDetailParser { */ fun parsePages(body: String): Int { return PATTERN_PAGES.find(body)?.groupValues?.get(1)?.toIntOrNull() - ?: throw ParseException("Parse pages error", body) + ?: throw ParseException("Parse pages error") } fun parsePreviewList(body: String): Pair, List> { diff --git a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryListParser.kt b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryListParser.kt index 7381df195..bb5788a4a 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryListParser.kt +++ b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryListParser.kt @@ -25,7 +25,7 @@ object GalleryListParser { if (body.contains("No hits found

")) { throw EhException(R.string.gallery_list_empty_hit) } - throw ParseException("Can't parse gallery list", body, it) + throw ParseException("Can't parse gallery list", it) } } diff --git a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryMultiPageViewerPTokenParser.kt b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryMultiPageViewerPTokenParser.kt index 845fa5d3e..628930854 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryMultiPageViewerPTokenParser.kt +++ b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryMultiPageViewerPTokenParser.kt @@ -33,7 +33,7 @@ object GalleryMultiPageViewerPTokenParser { imagelist.parseAs>().map(Item::token) }.getOrElse { ExceptionUtils.throwIfFatal(it) - throw ParseException("Parse pToken from MPV error", body, it) + throw ParseException("Parse pToken from MPV error", it) } } diff --git a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryPageApiParser.kt b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryPageApiParser.kt index 8ca00e06a..1722d8c98 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryPageApiParser.kt +++ b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryPageApiParser.kt @@ -27,7 +27,7 @@ object GalleryPageApiParser { private val PATTERN_ORIGIN_IMAGE_URL = Regex("") fun parse(body: String): Result { - runCatching { body.parseAs() }.onSuccess { throw ParseException(it.error, body) } + runCatching { body.parseAs() }.onSuccess { throw ParseException(it.error) } val res = body.parseAs() val imageUrl = PATTERN_IMAGE_URL.find(res.imageUrl)?.run { groupValues[1].trim().unescapeXml() @@ -41,7 +41,7 @@ object GalleryPageApiParser { if (!imageUrl.isNullOrEmpty()) { return Result(imageUrl, skipHathKey, originImageUrl) } else { - throw ParseException("Parse image url and skip hath key error", body) + throw ParseException("Parse image url and skip hath key error") } } diff --git a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryPageParser.kt b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryPageParser.kt index ef8e5a125..afd813f95 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryPageParser.kt +++ b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryPageParser.kt @@ -56,7 +56,7 @@ object GalleryPageParser { return if (!imageUrl.isNullOrEmpty() && !showKey.isNullOrEmpty()) { Result(imageUrl, skipHathKey, originImageUrl, showKey) } else { - throw ParseException("Parse image url and show error", body) + throw ParseException("Parse image url and show error") } } diff --git a/app/src/main/java/com/hippo/ehviewer/client/parser/HomeParser.kt b/app/src/main/java/com/hippo/ehviewer/client/parser/HomeParser.kt index 823f12a9b..d4b70e580 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/parser/HomeParser.kt +++ b/app/src/main/java/com/hippo/ehviewer/client/parser/HomeParser.kt @@ -24,7 +24,7 @@ object HomeParser { val fundsGP = ParserUtils.parseInt(get(2), 0) * 1000 return Funds(fundsGP, fundsC) } - throw ParseException("Parse funds error", body) + throw ParseException("Parse funds error") } @Parcelize diff --git a/app/src/main/java/com/hippo/ehviewer/client/parser/ProfileParser.kt b/app/src/main/java/com/hippo/ehviewer/client/parser/ProfileParser.kt index afbfc57e6..5e612c1d9 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/parser/ProfileParser.kt +++ b/app/src/main/java/com/hippo/ehviewer/client/parser/ProfileParser.kt @@ -46,7 +46,7 @@ object ProfileParser { Result(displayName, avatar) }.getOrElse { ExceptionUtils.throwIfFatal(it) - throw ParseException("Parse forums error", body) + throw ParseException("Parse forums error") } } diff --git a/app/src/main/java/com/hippo/ehviewer/client/parser/SignInParser.kt b/app/src/main/java/com/hippo/ehviewer/client/parser/SignInParser.kt index 1ad7bc990..0568f0546 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/parser/SignInParser.kt +++ b/app/src/main/java/com/hippo/ehviewer/client/parser/SignInParser.kt @@ -35,7 +35,7 @@ object SignInParser { if (m.find()) { throw EhException(m.group(1) ?: m.group(2)) } else { - throw ParseException("Can't parse sign in", body) + throw ParseException("Can't parse sign in") } } } diff --git a/app/src/main/java/com/hippo/ehviewer/client/parser/TorrentParser.kt b/app/src/main/java/com/hippo/ehviewer/client/parser/TorrentParser.kt index f4e880980..40f54df6e 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/parser/TorrentParser.kt +++ b/app/src/main/java/com/hippo/ehviewer/client/parser/TorrentParser.kt @@ -1,9 +1,12 @@ package com.hippo.ehviewer.client.parser +import com.hippo.ehviewer.client.exception.ParseException import java.nio.ByteBuffer object TorrentParser { - fun parse(body: ByteBuffer) = parseTorrent(body) + fun parse(body: ByteBuffer) = runCatching { + parseTorrent(body) + }.getOrElse { throw ParseException("Can't parse torrent list", it) } } class Torrent( diff --git a/app/src/main/java/com/hippo/ehviewer/util/AppConfig.kt b/app/src/main/java/com/hippo/ehviewer/util/AppConfig.kt index afeda9a5d..8147a3345 100644 --- a/app/src/main/java/com/hippo/ehviewer/util/AppConfig.kt +++ b/app/src/main/java/com/hippo/ehviewer/util/AppConfig.kt @@ -60,12 +60,12 @@ object AppConfig { return FileUtils.createTempFile(tempDir, null) } - fun saveParseErrorBody(e: ParseException) { + fun saveParseErrorBody(e: ParseException, body: String) { val dir = externalParseErrorDir ?: return File(dir, ReadableTime.getFilenamableTime(System.currentTimeMillis()) + ".txt").writer().use { it.write(e.message) it.appendLine() - it.append(e.body) + it.append(body) } } diff --git a/app/src/main/rust/src/parser/torrent.rs b/app/src/main/rust/src/parser/torrent.rs index 0fa140f33..38b6c9b2b 100644 --- a/app/src/main/rust/src/parser/torrent.rs +++ b/app/src/main/rust/src/parser/torrent.rs @@ -30,7 +30,7 @@ pub fn parseTorrent(env: JNIEnv, _class: JClass, buffer: JByteBuffer, limit: jin parse_bytebuffer(&mut env, buffer, limit, |dom, parser, _env| { Some(dom.query_selector("table")?.filter_map(|e| { let html = e.get(parser)?.inner_html(parser); - let grp = regex!(" ([0-9-]+) [0-9:]+[\\s\\S]+ ([0-9.]+ [KMGT]B)[\\s\\S]+ ([0-9]+)[\\s\\S]+ ([0-9]+)[\\s\\S]+ ([0-9]+)[\\s\\S]+([^<]+)[\\s\\S]+onclick=\"document.location='([^\"]+)'[^<]+>([^<]+)").captures(&html)?; + let grp = regex!(" ([0-9-]+) [0-9:]+[\\s\\S]+ ([0-9.]+ [KMGT]B)[\\s\\S]+ ([0-9]+)[\\s\\S]+ ([0-9]+)[\\s\\S]+ ([0-9]+)[\\s\\S]+([^<]+)[\\s\\S]+onclick=\"document.location='([^\"]+)'[^<]+>([^<]+)").captures(&html).unwrap(); let name = unescape(&grp[8]).ok()?; Some(Torrent { posted: grp[1].to_string(),