Skip to content

Commit

Permalink
fix: try catch
Browse files Browse the repository at this point in the history
  • Loading branch information
cssxsh committed Oct 20, 2022
1 parent 7ae9d48 commit 70c1089
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 23 deletions.
16 changes: 8 additions & 8 deletions src/main/kotlin/xyz/cssxsh/mirai/weibo/WeiboListener.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,20 @@ internal object WeiboListener : CoroutineScope {
logger.info { "${sender.render()} 匹配WEIBO(${result.value})" }
try {
message.quote() + client.getMicroBlog(mid = result.value).toMessage(contact = subject)
} catch (throwable: SerializationException) {
logger.warning({ "构建WEIBO(${result.value})序列化时失败" }, throwable)
} catch (exception: SerializationException) {
logger.warning({ "构建WEIBO(${result.value})序列化时失败" }, exception)
sendLoginMessage("构建WEIBO(${result.value})任务序列化时失败")
throwable.message
} catch (throwable: Throwable) {
logger.warning({ "构建WEIBO(${result.value})信息失败,尝试重新刷新" }, throwable)
exception.message
} catch (cause: Exception) {
logger.warning({ "构建WEIBO(${result.value})信息失败,尝试重新刷新" }, cause)
try {
client.restore()
null
} catch (cause: Throwable) {
logger.warning({ "WEIBO登陆状态失效,需要重新登陆, $cause" }, cause)
} catch (cause: Exception) {
logger.warning({ "WEIBO登陆状态失效,需要重新登陆" }, cause)
sendLoginMessage("WEIBO登陆状态失效,需要重新登陆 /wlogin")
cause.message
} ?: throwable.message
} ?: cause.message
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/xyz/cssxsh/mirai/weibo/WeiboSubscriber.kt
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,11 @@ abstract class WeiboSubscriber<K : Comparable<K>>(val type: String) : CoroutineS
continue
} catch (exception: UnknownHostException) {
//
} catch (exception: Throwable) {
} catch (exception: Exception) {
logger.warning({ "WEIBO登陆状态将刷新" }, exception)
try {
client.restore()
} catch (_: Throwable) {
} catch (_: Exception) {
//
}
} finally {
Expand Down
14 changes: 7 additions & 7 deletions src/main/kotlin/xyz/cssxsh/mirai/weibo/WeiboUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,13 @@ internal fun sendLoginMessage(message: String) {
try {
WeiboHelperPlugin
} catch (_: Throwable) {
CoroutineScope(Dispatchers.IO)
}.launch(SupervisorJob()) {
CoroutineScope(Dispatchers.IO) + SupervisorJob()
}.launch {
while (isActive) {
try {
LoginContact.sendMessage(message)
break
} catch (cause: Throwable) {
} catch (cause: Exception) {
logger.warning({ "${LoginContact.render()} 发送消息失败" }, cause)
}
delay(60_000L)
Expand Down Expand Up @@ -473,10 +473,10 @@ internal suspend fun restore(interval: Long = 600_000) = supervisorScope {
val result = client.restore()
logger.info { "WEIBO登陆状态已刷新 $result" }
continue
} catch (throwable: SerializationException) {
logger.warning({ "WEIBO RESTORE 任务序列化时失败" }, throwable)
} catch (exception: SerializationException) {
logger.warning({ "WEIBO RESTORE 任务序列化时失败" }, exception)
sendLoginMessage("WEIBO RESTORE 任务序列化时失败")
} catch (cause: Throwable) {
} catch (cause: Exception) {
logger.warning({ "WEIBO登陆状态失效,需要重新登陆" }, cause)
sendLoginMessage("WEIBO登陆状态失效,需要重新登陆 /wlogin")
}
Expand Down Expand Up @@ -565,7 +565,7 @@ internal suspend fun <T : CommandSenderOnMessage<*>> T.quote(block: suspend T.(C
return try {
quoteReply(block(fromEvent.subject))
true
} catch (cause: Throwable) {
} catch (cause: Exception) {
logger.warning({ "发送消息失败" }, cause)
quoteReply("发送消息失败, ${cause.message}")
false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ object WeiboLoginCommand : SimpleCommand(
withTimeout(60_000) {
client.download(url).toExternalResource().use { it.uploadAsImage(contact) }
}
} catch (_: Throwable) {
} catch (_: Exception) {
url.toPlainText()
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/xyz/cssxsh/weibo/Load.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ suspend inline fun <reified T> WeiboClient.callback(url: String, crossinline blo
val json = text(url, block).substringAfter('(').substringBefore(')')
return try {
WeiboClient.Json.decodeFromString(json)
} catch (cause: Throwable) {
} catch (cause: Exception) {
throw IllegalArgumentException(json, cause)
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/main/kotlin/xyz/cssxsh/weibo/WeiboClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import kotlinx.serialization.json.*
import xyz.cssxsh.weibo.data.*
import java.io.IOException
import kotlin.coroutines.*
import kotlin.coroutines.cancellation.*

open class WeiboClient(val ignore: suspend (Throwable) -> Boolean = DefaultIgnore) : CoroutineScope, Closeable {
override val coroutineContext: CoroutineContext
Expand Down Expand Up @@ -79,14 +78,16 @@ open class WeiboClient(val ignore: suspend (Throwable) -> Boolean = DefaultIgnor

suspend fun <T> useHttpClient(block: suspend (HttpClient) -> T): T = supervisorScope {
var count = 0
var cause: Throwable? = null
while (isActive) {
try {
return@supervisorScope block(client)
} catch (cause: Throwable) {
} catch (throwable: Throwable) {
cause = throwable
count++
if (count > max || ignore(cause).not()) throw cause
if (count > max || ignore(throwable).not()) throw throwable
}
}
throw CancellationException(null)
throw CancellationException(null, cause)
}
}

0 comments on commit 70c1089

Please sign in to comment.