Skip to content

Commit 77fc011

Browse files
committed
chore: fix serialize issues & upgrade kotlin, gradle version
1 parent c416eb8 commit 77fc011

File tree

13 files changed

+96
-63
lines changed

13 files changed

+96
-63
lines changed

build.gradle.kts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
2-
31
plugins {
4-
kotlin("jvm") version "1.9.10"
2+
kotlin("jvm") version "1.9.24"
53
`maven-publish`
6-
kotlin("plugin.serialization") version "1.9.10"
4+
kotlin("plugin.serialization") version "1.9.24"
75
id("org.jetbrains.dokka") version "0.9.17"
86
}
97

@@ -16,10 +14,10 @@ repositories {
1614
}
1715

1816
object Versions {
19-
const val jackson = "2.14.1"
17+
const val jackson = "2.18.0"
2018
const val log4j = "2.17.2"
2119
const val disruptor = "3.4.4"
22-
const val ktor = "2.3.0"
20+
const val ktor = "2.3.12"
2321
}
2422

2523
dependencies {
@@ -33,9 +31,9 @@ dependencies {
3331
api("com.fasterxml.jackson.datatype:jackson-datatype-jdk8:${Versions.jackson}")
3432
api("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:${Versions.jackson}")
3533

36-
api("io.ktor:ktor-client-core:${Versions.ktor}")
37-
api("io.ktor:ktor-client-okhttp:${Versions.ktor}")
38-
api("io.ktor:ktor-client-encoding:${Versions.ktor}")
34+
api("io.ktor:ktor-client-core-jvm:${Versions.ktor}")
35+
api("io.ktor:ktor-client-okhttp-jvm:${Versions.ktor}")
36+
api("io.ktor:ktor-client-encoding-jvm:${Versions.ktor}")
3937

4038
api(platform("io.projectreactor:reactor-bom:2020.0.18"))
4139
api("io.projectreactor:reactor-core")
@@ -47,7 +45,3 @@ java {
4745
withJavadocJar()
4846
withSourcesJar()
4947
}
50-
51-
tasks.withType<KotlinCompile>() {
52-
kotlinOptions.jvmTarget = "17"
53-
}

gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip
44
networkTimeout=10000
55
validateDistributionUrl=true
66
zipStoreBase=GRADLE_USER_HOME

gradlew

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,15 @@ if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
145145
case $MAX_FD in #(
146146
max*)
147147
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
148-
# shellcheck disable=SC3045
148+
# shellcheck disable=SC2039,SC3045
149149
MAX_FD=$( ulimit -H -n ) ||
150150
warn "Could not query maximum file descriptor limit"
151151
esac
152152
case $MAX_FD in #(
153153
'' | soft) :;; #(
154154
*)
155155
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
156-
# shellcheck disable=SC3045
156+
# shellcheck disable=SC2039,SC3045
157157
ulimit -n "$MAX_FD" ||
158158
warn "Could not set maximum file descriptor limit to $MAX_FD"
159159
esac
@@ -202,11 +202,11 @@ fi
202202
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
203203
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
204204

205-
# Collect all arguments for the java command;
206-
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
207-
# shell script including quotes and variable substitutions, so put them in
208-
# double quotes to make sure that they get re-expanded; and
209-
# * put everything else in single quotes, so that it's not re-expanded.
205+
# Collect all arguments for the java command:
206+
# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
207+
# and any embedded shellness will be escaped.
208+
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
209+
# treated as '${Hostname}' itself on the command line.
210210

211211
set -- \
212212
"-Dorg.gradle.appname=$APP_BASE_NAME" \

src/main/kotlin/moe/kurenai/bgm/BgmClient.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,15 @@ class BgmClient(
102102
}
103103

104104
internal fun buildAccessTokenReq(grantType: String, code: String? = null, refreshToken: String? = null, state: String? = null): AccessTokenRequest {
105-
return AccessTokenRequest(grantType, appId, appSecret, redirectUri, code, refreshToken, state)
105+
return AccessTokenRequest(
106+
grantType = grantType,
107+
clientId = appId,
108+
clientSecret = appSecret,
109+
redirectUri = redirectUri,
110+
code = code,
111+
refreshToken = refreshToken,
112+
state = state
113+
)
106114
}
107115

108116
internal fun determineUri(request: Request<*>): String {

src/main/kotlin/moe/kurenai/bgm/CoroutineBgmClient.kt

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ import io.ktor.http.*
99
import kotlinx.serialization.DeserializationStrategy
1010
import kotlinx.serialization.json.Json
1111
import moe.kurenai.bgm.exception.BgmException
12-
import moe.kurenai.bgm.exception.NotFoundException
13-
import moe.kurenai.bgm.exception.UnauthorizedException
14-
import moe.kurenai.bgm.exception.ValidationError
1512
import moe.kurenai.bgm.model.auth.AccessToken
13+
import moe.kurenai.bgm.model.error.BgmError
14+
import moe.kurenai.bgm.model.error.NotFoundError
15+
import moe.kurenai.bgm.model.error.UnauthorizedError
16+
import moe.kurenai.bgm.model.error.ValidationError
1617
import moe.kurenai.bgm.request.Request
1718
import moe.kurenai.bgm.request.auth.AccessTokenGrantType
1819
import moe.kurenai.bgm.util.DefaultMapper.convertToByteArray
@@ -61,7 +62,8 @@ class CoroutineBgmClient(
6162
}
6263
}
6364
val deserializer =
64-
request.responseDeserializer ?: throw BgmException("[${request.javaClass.name}]没有声明返回值反序列化对象")
65+
request.responseDeserializer
66+
?: throw BgmException(BgmError("[${request.javaClass.name}]没有声明返回值反序列化对象"))
6567
return resp.parse(deserializer)
6668
}
6769

@@ -88,34 +90,45 @@ class CoroutineBgmClient(
8890
return when (this.status) {
8991
HttpStatusCode.OK -> {
9092
kotlin.runCatching { json.decodeFromString(deserializer, body) }
91-
.recover {
92-
throw kotlin.runCatching {
93-
json.decodeFromString(BgmException.serializer(), body).apply {
94-
cause = it
93+
.recoverCatching { ex ->
94+
json.decodeFromString(BgmError.serializer(), body).let {
95+
BgmException(it).apply {
96+
cause = ex
9597
}
96-
}.getOrNull() ?: it
98+
}
99+
}.recover { ex ->
100+
BgmException(BgmError("Unknown response type: $body")).apply { cause = ex }
97101
}.getOrThrow()
98102
}
99103

100104
HttpStatusCode.Unauthorized -> {
101-
throw json.decodeFromString(UnauthorizedException.serializer(), body)
105+
json.decodeFromString(UnauthorizedError.serializer(), body)
102106
}
103107

104108
HttpStatusCode.NotFound -> {
105-
throw json.decodeFromString(NotFoundException.serializer(), body)
109+
json.decodeFromString(NotFoundError.serializer(), body)
106110
}
107111

108112
HttpStatusCode.UnprocessableEntity -> {
109-
throw json.decodeFromString(ValidationError.serializer(), body)
113+
json.decodeFromString(ValidationError.serializer(), body)
110114
}
111115

112116
else -> {
113-
throw runCatching {
114-
json.decodeFromString(BgmException.serializer(), body)
117+
runCatching {
118+
json.decodeFromString(BgmError.serializer(), body)
115119
}.recover {
116-
BgmException("Unknown response type")
120+
BgmException(BgmError(error = "Unknown response type: $body")).apply {
121+
this.cause = it
122+
}
117123
}.getOrThrow()
118124
}
125+
}.let {
126+
return@let when (it) {
127+
is BgmError -> throw BgmException(it)
128+
is BgmException -> throw it
129+
is Throwable -> throw BgmException(BgmError("Unknown response type: $body")).apply { cause = it }
130+
else -> it as T
131+
}
119132
}
120133
}
121134

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
package moe.kurenai.bgm.exception
22

3-
import kotlinx.serialization.SerialName
4-
import kotlinx.serialization.Serializable
5-
import kotlinx.serialization.Transient
3+
import moe.kurenai.bgm.model.error.BgmError
64

7-
@Serializable
8-
open class BgmException(
9-
val error: String = "",
10-
@SerialName("error_description")
11-
val errorDescription: String = "",
12-
val request: String = "",
13-
val code: Int = 0,
14-
) : RuntimeException("$error - $errorDescription") {
15-
@Transient
5+
class BgmException(
6+
val error: BgmError
7+
) : Exception("${error.error} - ${error.errorDescription}") {
168
override var cause: Throwable? = null
179
}

src/main/kotlin/moe/kurenai/bgm/model/auth/AccessToken.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import kotlinx.serialization.Serializable
55

66

77
@Serializable
8-
class AccessToken(
8+
data class AccessToken(
99
@SerialName("access_token")
1010
val accessToken: String,
1111
@SerialName("expires_in")
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package moe.kurenai.bgm.model.error
2+
3+
import kotlinx.serialization.SerialName
4+
import kotlinx.serialization.Serializable
5+
6+
@Serializable
7+
open class BgmError(
8+
val error: String = "",
9+
@SerialName("error_description")
10+
val errorDescription: String = "",
11+
val request: String = "",
12+
val code: Int = 0,
13+
)
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
package moe.kurenai.bgm.exception
1+
package moe.kurenai.bgm.model.error
22

33
import kotlinx.serialization.Serializable
44

55
@Serializable
6-
class NotFoundException(
6+
class NotFoundError(
77
var title: String? = null,
88
var description: String? = null,
99
var detail: Map<String, String>? = null,
10-
): BgmException() {
10+
) : BgmError() {
1111
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
package moe.kurenai.bgm.exception
1+
package moe.kurenai.bgm.model.error
22

33
import kotlinx.serialization.Serializable
44

55
@Serializable
6-
class UnauthorizedException(
6+
class UnauthorizedError(
77
var title: String? = null,
88
var description: String? = null,
99
var detail: Map<String, String>? = null,
10-
) : BgmException() {
10+
) : BgmError() {
1111
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
package moe.kurenai.bgm.exception
1+
package moe.kurenai.bgm.model.error
22

33
import kotlinx.serialization.Serializable
44

55
@Serializable
66
class ValidationError(
77
var detail: Map<String, String>? = null,
8-
): BgmException() {
8+
) : BgmError() {
99

1010
}

src/main/kotlin/moe/kurenai/bgm/request/auth/GetAccessToken.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package moe.kurenai.bgm.request.auth
33
import com.fasterxml.jackson.annotation.JsonIgnore
44
import com.fasterxml.jackson.core.type.TypeReference
55
import kotlinx.serialization.DeserializationStrategy
6+
import kotlinx.serialization.SerialName
67
import kotlinx.serialization.Serializable
78
import kotlinx.serialization.Transient
89
import moe.kurenai.bgm.model.auth.AccessToken
@@ -11,11 +12,16 @@ import moe.kurenai.bgm.request.Request
1112

1213
@Serializable
1314
class AccessTokenRequest(
15+
@SerialName("grant_type")
1416
val grantType: String,
17+
@SerialName("client_id")
1518
val clientId: String,
19+
@SerialName("client_secret")
1620
val clientSecret: String,
21+
@SerialName("redirect_uri")
1722
val redirectUri: String,
1823
val code: String? = null,
24+
@SerialName("refresh_token")
1925
val refreshToken: String? = null,
2026
val state: String? = null,
2127
): Request<AccessToken>() {

src/main/kotlin/moe/kurenai/bgm/util/DefaultMapper.kt

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ import com.fasterxml.jackson.module.kotlin.KotlinFeature
99
import com.fasterxml.jackson.module.kotlin.jsonMapper
1010
import com.fasterxml.jackson.module.kotlin.kotlinModule
1111
import moe.kurenai.bgm.exception.BgmException
12-
import moe.kurenai.bgm.exception.NotFoundException
13-
import moe.kurenai.bgm.exception.ValidationError
12+
import moe.kurenai.bgm.model.error.BgmError
13+
import moe.kurenai.bgm.model.error.NotFoundError
14+
import moe.kurenai.bgm.model.error.ValidationError
1415
import java.io.IOException
1516
import java.net.http.HttpResponse
1617

@@ -58,15 +59,21 @@ object DefaultMapper {
5859
}
5960

6061
404 -> {
61-
throw MAPPER.readValue(response.body(), NotFoundException::class.java)
62+
MAPPER.readValue(response.body(), NotFoundError::class.java)
6263
}
6364

6465
422 -> {
65-
throw MAPPER.readValue(response.body(), ValidationError::class.java)
66+
MAPPER.readValue(response.body(), ValidationError::class.java)
6667
}
6768

6869
else -> {
69-
throw BgmException("Unknown response type")
70+
BgmError("Unknown response type: ${response.body()}")
71+
}
72+
}.let {
73+
if (it is BgmError) {
74+
throw BgmException(it)
75+
} else {
76+
it as T
7077
}
7178
}
7279
}

0 commit comments

Comments
 (0)