kotlin - 在 ktor HTTPClient 中解析错误正文
问题描述
我有一个 api,它在发送错误请求时返回带有正确错误信息的错误正文。例如,我得到状态码 400 和以下正文 -
{
"errorCode": 1011,
"errorMessage": "Unable to get Child information"
}
现在,当我为此在多平台模块中编写 ktor 客户端时,我会在响应验证器中捕获它,例如 -
HttpResponseValidator {
validateResponse {
val statusCode = it.status.value
when (statusCode) {
in 300..399 -> print(it.content.toString())
in 400..499 -> {
print(it.content.toString())
throw ClientRequestException(it)
}
in 500..599 -> print(it.content.toString())
}
}
handleResponseException {
print(it.message)
}
}
我在这里的查询是我无法访问validateResponse
或中的响应错误正文handleResponseException
。有没有办法我可以捕获并解析它以获取服务器发送的实际错误?
解决方案
您可以声明一个数据类 Error 来表示您期望的错误响应。
import kotlinx.serialization.Serializable
@Serializable
data class Error(
val errorCode: Int, //if by errorCode you mean the http status code is not really necessary to include here as you already know it from the validateResponse
val errorMessage: String
)
您可以享受暂停的乐趣来解析响应并将其作为错误数据类的实例
suspend fun getError(responseContent: ByteReadChannel): Error {
responseContent.readUTF8Line()?.let {
return Json(JsonConfiguration.Stable).parse(Error.serializer(), it)
}
throw IllegalArgumentException("not a parsable error")
}
然后在handleResponseException里面
handleResponseException { cause ->
val error = when (cause) {
is ClientRequestException -> exceptionHandler.getError(cause.response.content)
// other cases here
else -> // throw Exception() do whatever you need
}
//resume with the error
}
例如,您可以根据引发异常并在代码中的其他位置捕获它的错误来实现一些逻辑
when (error.errorCode) {
1-> throw MyCustomException(error.errorMessage)
else -> throw Exception(error.errorMessage)
}
我希望它有帮助