From cc136cd3b10f88fbe71118106a78904821556f29 Mon Sep 17 00:00:00 2001 From: Ziedelth Date: Thu, 19 Dec 2024 19:54:51 +0100 Subject: [PATCH] improved errors logs on social network wrappers --- .../kotlin/fr/shikkanime/wrappers/BskyWrapper.kt | 16 ++++++++-------- .../fr/shikkanime/wrappers/ThreadsWrapper.kt | 16 ++++++---------- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/main/kotlin/fr/shikkanime/wrappers/BskyWrapper.kt b/src/main/kotlin/fr/shikkanime/wrappers/BskyWrapper.kt index 30938a1e..c2aec86a 100644 --- a/src/main/kotlin/fr/shikkanime/wrappers/BskyWrapper.kt +++ b/src/main/kotlin/fr/shikkanime/wrappers/BskyWrapper.kt @@ -9,8 +9,8 @@ import org.jsoup.Jsoup import java.time.ZoneId import java.time.ZonedDateTime import java.time.format.DateTimeFormatter +import java.util.regex.MatchResult import java.util.regex.Pattern -import java.util.stream.Collectors private const val TYPE = "\$type" @@ -57,7 +57,7 @@ object BskyWrapper { ) ) - require(response.status.value == 200) { "Failed to create session" } + require(response.status == HttpStatusCode.OK) { "Failed to create session (${response.status.value} - ${response.bodyAsText()})" } return ObjectParser.fromJson(response.bodyAsText()) } @@ -73,7 +73,7 @@ object BskyWrapper { body = content, ) - require(response.status.value == 200) { "Failed to upload blob" } + require(response.status == HttpStatusCode.OK) { "Failed to upload blob (${response.status.value} - ${response.bodyAsText()})" } return ObjectParser.fromJson(response.bodyAsText()).getAsJsonObject("blob") } @@ -165,14 +165,14 @@ object BskyWrapper { ) ) - require(response.status.value == 200) { "Failed to create record (${response.bodyAsText()})" } + require(response.status == HttpStatusCode.OK) { "Failed to create record (${response.status.value} - ${response.bodyAsText()})" } return ObjectParser.fromJson(response.bodyAsText(), Record::class.java) } - private fun countEmoji(text: String) = Pattern.compile("\\p{So}+") + private fun countEmoji(text: String): List = Pattern.compile("\\p{So}+") .matcher(text) .results() - .collect(Collectors.toUnmodifiableList()) + .toList() private fun getFacets(text: String): Pair> { var tmpText = text @@ -185,7 +185,7 @@ object BskyWrapper { val beautifulLink = link.replace("https?://www\\.|\\?.*".toRegex(), "").trim() tmpText = tmpText.replace(link, beautifulLink) - val emojiCount = countEmoji(tmpText.substringBeforeLast(beautifulLink))!! + val emojiCount = countEmoji(tmpText.substringBeforeLast(beautifulLink)) val added = if (emojiCount.isNotEmpty()) emojiCount.sumOf { it.group().length } + emojiCount.size else 0 @@ -195,7 +195,7 @@ object BskyWrapper { } link.startsWith("#") -> { - val emojiCount = countEmoji(tmpText.substringBeforeLast(link))!! + val emojiCount = countEmoji(tmpText.substringBeforeLast(link)) val added = if (emojiCount.isNotEmpty()) emojiCount.sumOf { it.group().length } + emojiCount.size else 1 diff --git a/src/main/kotlin/fr/shikkanime/wrappers/ThreadsWrapper.kt b/src/main/kotlin/fr/shikkanime/wrappers/ThreadsWrapper.kt index 9e0c587f..0464e337 100644 --- a/src/main/kotlin/fr/shikkanime/wrappers/ThreadsWrapper.kt +++ b/src/main/kotlin/fr/shikkanime/wrappers/ThreadsWrapper.kt @@ -40,10 +40,8 @@ object ThreadsWrapper { headers = mapOf(HttpHeaders.ContentType to ContentType.Application.Json.toString()), ) - require(response.status == HttpStatusCode.OK) { "Failed to get token" } - - val json = ObjectParser.fromJson(response.bodyAsText()) - return json["access_token"].asString + require(response.status == HttpStatusCode.OK) { "Failed to get token (${response.status.value} - ${response.bodyAsText()})" } + return ObjectParser.fromJson(response.bodyAsText())["access_token"].asString } suspend fun getLongLivedAccessToken(appSecret: String, accessToken: String): String { @@ -54,10 +52,8 @@ object ThreadsWrapper { "grant_type=th_exchange_token", ) - require(response.status == HttpStatusCode.OK) { "Failed to get long-lived token" } - - val json = ObjectParser.fromJson(response.bodyAsText()) - return json["access_token"].asString + require(response.status == HttpStatusCode.OK) { "Failed to get long-lived token (${response.status.value} - ${response.bodyAsText()})" } + return ObjectParser.fromJson(response.bodyAsText())["access_token"].asString } suspend fun post( @@ -82,7 +78,7 @@ object ThreadsWrapper { headers = mapOf(HttpHeaders.ContentType to ContentType.Application.Json.toString()), ) - require(createResponse.status == HttpStatusCode.OK) { "Failed to post" } + require(createResponse.status == HttpStatusCode.OK) { "Failed to post (${createResponse.status.value} - ${createResponse.bodyAsText()})" } val creationId = ObjectParser.fromJson(createResponse.bodyAsText())["id"].asString val publishResponse = httpRequest.post( @@ -92,7 +88,7 @@ object ThreadsWrapper { headers = mapOf(HttpHeaders.ContentType to ContentType.Application.Json.toString()), ) - require(publishResponse.status == HttpStatusCode.OK) { "Failed to publish" } + require(publishResponse.status == HttpStatusCode.OK) { "Failed to publish (${publishResponse.status.value} - ${publishResponse.bodyAsText()})" } return ObjectParser.fromJson(publishResponse.bodyAsText())["id"].asLong } } \ No newline at end of file