Check temporary IP ban and refine error translation (#262)

* Check temporary IP ban and refine error translation

* Fix lint
This commit is contained in:
FooIbar authored and GitHub committed 2023-11-14 10:27:45 +00:00
1 parent 07009f8bd6
commit 237938b1f0
5 files changed
+19 -4

No files matched your search

@@ -61,6 +61,7 @@ import java.nio.ByteBuffer
import kotlin.math.ceil
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.delay
import kotlinx.serialization.SerializationException
import kotlinx.serialization.json.add
import kotlinx.serialization.json.addJsonArray
import kotlinx.serialization.json.put
@@ -106,7 +107,7 @@ private fun rethrowExactly(code: Int, body: String, e: Throwable): Nothing {
throw StatusCodeException(code)
}
if (e is ParseException) {
if (e is ParseException || e is SerializationException) {
if ("<" !in body) {
throw EhException(body)
} else {
@@ -142,6 +143,11 @@ suspend fun <T> fetchCompat(
cronetRequest(url, referer, origin) {
reqBody?.let { withRequestBody(it) }
}.execute {
@Suppress("NewApi")
val code = it.httpStatusCode
if (code >= 400) {
throw StatusCodeException(code)
}
httpContentPool.useInstance { buffer ->
awaitBodyFully(buffer)
buffer.flip()
@@ -16,6 +16,9 @@ object GalleryListParser {
parseGalleryInfoList(body)
}.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)
}
@@ -16,7 +16,6 @@
package com.hippo.ehviewer.util
import android.os.Environment
import com.hippo.ehviewer.client.exception.ParseException
import java.io.File
import splitties.init.appCtx
@@ -60,7 +59,7 @@ object AppConfig {
return FileUtils.createTempFile(tempDir, null)
}
fun saveParseErrorBody(e: ParseException, body: String) {
fun saveParseErrorBody(e: Throwable, body: String) {
val dir = externalParseErrorDir ?: return
File(dir, ReadableTime.getFilenamableTime(System.currentTimeMillis()) + ".txt").writer().use {
it.write(e.message)
+6
View File
@@ -33,6 +33,12 @@ macro_rules! regex {
const EHGT_PREFIX: &str = "https://ehgt.org/";
const EX_PREFIX: &str = "https://s.exhentai.org/";
fn check_html(str: &str) {
if !str.contains('<') {
panic!("{}", str)
}
}
fn get_vdom_first_element_by_class_name<'a>(dom: &'a VDom, name: &str) -> Option<&'a Node<'a>> {
let handle = dom.get_elements_by_class_name(name).next()?;
handle.get(dom.parser())
+2 -1
View File
@@ -5,11 +5,11 @@ use jnix::jni::sys::{jint, jobject};
use jnix::jni::JNIEnv;
use jnix::{IntoJava, JnixEnv};
use jnix_macros::IntoJava;
use parse_bytebuffer;
use quick_xml::escape::unescape;
use std::borrow::Cow;
use std::ops::Index;
use tl::{Node, Parser};
use {check_html, parse_bytebuffer};
use {get_element_by_id, get_vdom_first_element_by_class_name};
use {get_first_element_by_class_name, query_childs_first_match_attr};
use {get_node_attr, get_node_handle_attr, regex};
@@ -205,6 +205,7 @@ pub fn parseGalleryInfoList(
) -> jobject {
let mut env = JnixEnv { env };
parse_bytebuffer(&mut env, buffer, limit, |dom, parser, _, str| {
check_html(str);
if str.contains("<p>You do not have any watched tags") {
panic!("No watched tags!")
}