From 45b843fadc0c2b07e678d110387688ef8113d63a Mon Sep 17 00:00:00 2001 From: Koitharu Date: Sun, 2 Feb 2025 10:14:48 +0200 Subject: [PATCH] Replace try-catch with use{} in parse functions --- .../koitharu/kotatsu/parsers/ErrorMessages.kt | 1 + .../koitharu/kotatsu/parsers/util/Parse.kt | 38 ++++++++----------- 2 files changed, 16 insertions(+), 23 deletions(-) diff --git a/src/main/kotlin/org/koitharu/kotatsu/parsers/ErrorMessages.kt b/src/main/kotlin/org/koitharu/kotatsu/parsers/ErrorMessages.kt index d20d20ff..fceffaf7 100644 --- a/src/main/kotlin/org/koitharu/kotatsu/parsers/ErrorMessages.kt +++ b/src/main/kotlin/org/koitharu/kotatsu/parsers/ErrorMessages.kt @@ -15,4 +15,5 @@ public object ErrorMessages { public const val FILTER_BOTH_STATES_GENRES_NOT_SUPPORTED: String = "Filtering by both genres and states is not supported by this source" public const val SEARCH_NOT_SUPPORTED: String = "Search is not supported by this source" + public const val RESPONSE_NULL_BODY: String = "Response has no body" } diff --git a/src/main/kotlin/org/koitharu/kotatsu/parsers/util/Parse.kt b/src/main/kotlin/org/koitharu/kotatsu/parsers/util/Parse.kt index 857b2882..6b734a1a 100644 --- a/src/main/kotlin/org/koitharu/kotatsu/parsers/util/Parse.kt +++ b/src/main/kotlin/org/koitharu/kotatsu/parsers/util/Parse.kt @@ -4,11 +4,11 @@ package org.koitharu.kotatsu.parsers.util import okhttp3.Response import okhttp3.ResponseBody -import okhttp3.internal.closeQuietly import org.json.JSONArray import org.json.JSONObject import org.jsoup.Jsoup import org.jsoup.nodes.Document +import org.koitharu.kotatsu.parsers.ErrorMessages import org.koitharu.kotatsu.parsers.InternalParsersApi import java.text.DateFormat @@ -21,12 +21,10 @@ internal const val SCHEME_HTTPS = "https" * @see [parseJsonArray] */ // TODO suspend -public fun Response.parseHtml(): Document = try { - val body = requireBody() +public fun Response.parseHtml(): Document = use { response -> + val body = response.requireBody() val charset = body.contentType()?.charset()?.name() - Jsoup.parse(body.byteStream(), charset, request.url.toString()) -} finally { - closeQuietly() + Jsoup.parse(body.byteStream(), charset, response.request.url.toString()) } /** @@ -34,10 +32,8 @@ public fun Response.parseHtml(): Document = try { * @see [parseJsonArray] * @see [parseHtml] */ -public fun Response.parseJson(): JSONObject = try { - JSONObject(requireBody().string()) -} finally { - closeQuietly() +public fun Response.parseJson(): JSONObject = use { response -> + JSONObject(response.requireBody().string()) } /** @@ -45,22 +41,16 @@ public fun Response.parseJson(): JSONObject = try { * @see [parseJson] * @see [parseHtml] */ -public fun Response.parseJsonArray(): JSONArray = try { - JSONArray(requireBody().string()) -} finally { - closeQuietly() +public fun Response.parseJsonArray(): JSONArray = use { response -> + JSONArray(response.requireBody().string()) } -public fun Response.parseRaw(): String = try { - requireBody().string() -} finally { - closeQuietly() +public fun Response.parseRaw(): String = use { response -> + response.requireBody().string() } -public fun Response.parseBytes(): ByteArray = try { - requireBody().bytes() -} finally { - closeQuietly() +public fun Response.parseBytes(): ByteArray = use { response -> + response.requireBody().bytes() } /** @@ -110,4 +100,6 @@ public fun DateFormat.tryParse(str: String?): Long = if (str.isNullOrEmpty()) { }.getOrDefault(0L) } -public fun Response.requireBody(): ResponseBody = requireNotNull(body) { "Response body is null" } +public fun Response.requireBody(): ResponseBody = requireNotNull(body) { + ErrorMessages.RESPONSE_NULL_BODY +}