Skip to content
This repository has been archived by the owner on Sep 12, 2021. It is now read-only.

Commit

Permalink
Try continue when request invalid.
Browse files Browse the repository at this point in the history
  • Loading branch information
Karlatemp committed Oct 28, 2020
1 parent a63b60d commit b2e1591
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 16 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 2.0.0-dev-3

- 更新到 `MiraiConsole 1.0-RC`
- 在解析请求错误时尝试不直接断开连接

# 2.0.0-dev-2
修复 [#2](https://github.com/Karlatemp/mirai-websocket-api/issues/2)

Expand Down
11 changes: 6 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ static ktor(id) {
dependencies {
// https://mvnrepository.com/artifact/com.google.guava/guava
compile group: 'com.google.guava', name: 'guava', version: '29.0-jre'
def miraiConsoleVersion = project.property('mirai.console.version')

compileOnly("net.mamoe:mirai-core:1.3.1")
compileOnly("net.mamoe:mirai-console:1.0-RC-dev-28")
testCompile("net.mamoe:mirai-core:1.3.1")
testCompile("net.mamoe:mirai-console:1.0-RC-dev-28")
testCompile("net.mamoe:mirai-console-terminal:1.0-RC-dev-28")
compileOnly("net.mamoe:mirai-core:1.3.2")
compileOnly("net.mamoe:mirai-console:$miraiConsoleVersion")
testCompile("net.mamoe:mirai-core:1.3.2")
testCompile("net.mamoe:mirai-console:$miraiConsoleVersion")
testCompile("net.mamoe:mirai-console-terminal:$miraiConsoleVersion")

def autoService = '1.0-rc7'
kapt "com.google.auto.service:auto-service:$autoService"
Expand Down
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
# mirai-websocket-api/gradle.properties
#
kotlin.code.style=official
wsapi.version=2.0.0-dev-2
wsapi.version=2.0.0-dev-3
mirai.console.version=1.0-RC

Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ import io.ktor.server.engine.*
import io.ktor.util.*
import io.ktor.websocket.*
import kotlinx.serialization.SerializationStrategy
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.put
import kotlinx.serialization.json.*
import net.mamoe.mirai.Bot
import net.mamoe.mirai.console.plugin.jvm.JvmPlugin
import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescriptionBuilder
Expand Down Expand Up @@ -177,11 +175,13 @@ fun Application.web() {
rep(OutgoingSerializer, ActionResult(this?.requestId, true, null, null, ext))

suspend fun Request?.repOk() = repOk(null)
suspend fun repResult(result: ActionResult) = rep(
OutgoingSerializer, result
)

suspend fun Request?.repErr(
error: String, full: String
) = rep(
OutgoingSerializer,
) = repResult(
ActionResult(this?.requestId, false, error, full, null)
)

Expand Down Expand Up @@ -218,12 +218,32 @@ fun Application.web() {

for (frame in incoming) {
if (frame is Frame.Text) {
val request = kotlin.runCatching {
json.decodeFromString(Request.RequestSerializer, frame.readText())
}.getOrElse { exception ->
null.repErr(exception)
return@webSocket
val requestContent = frame.readText()
val requestBox = kotlin.runCatching {
json.decodeFromString(Request.RequestSerializer, requestContent)
}
if (requestBox.isFailure) {
val json = kotlin.runCatching {
json.decodeFromString(JsonObject.serializer(), requestContent)
}.getOrNull()
val error = requestBox.exceptionOrNull()!!
if (json == null) {
null.repErr(error)
return@webSocket
} else {
repResult(
ActionResult(
(json["requestId"] as? JsonPrimitive)?.content,
false,
error.toString(),
error.stackTraceToString(),
null
)
)
continue
}
}
val request = requestBox.getOrThrow()
val action = request.action

try {
Expand Down

0 comments on commit b2e1591

Please sign in to comment.