如何在 ktor 的 api 调用响应中获取错误消息
How to get error message in api call response in ktor
我正在学习 Ktor。我想打印错误值或异常。我从这个 中提取了一些代码。我不完全理解这个 post 答案。
ApiResponse.kt
sealed class ApiResponse<out T : Any> {
data class Success<out T : Any>(
val data: T?
) : ApiResponse<T>()
data class Error(
val responseCode: Int = -1,
) : ApiResponse<Nothing>()
fun handleResult(onSuccess: ((responseData: T?) -> Unit)?, onError: ((error: Error) -> Unit)?) {
when (this) {
is Success -> {
onSuccess?.invoke(this.data)
}
is Error -> {
onError?.invoke(this)
}
}
}
}
@Serializable
data class ErrorResponse(
var errorCode: Int = 1,
val errorMessage: String = "Something went wrong"
)
KtorApi.kt
class KtorApi(private val httpClient: HttpClient) : NetworkRoute() {
suspend fun getCat(): Response<CatResponse> {
val response = httpClient.get {
url("https://xyz/cat")
}
return apiCall(response)
}
}
CatResponse.kt
@Serializable
data class CatResponse(
val items: List<CatDetails>? = null
)
@Serializable
data class CatDetails(
val id: String? = null,
val name: String? = null,
)
ViewModel.kt
fun getCat() {
viewModelScope.launch {
KtorApi.getCat().handleResult({ data ->
logE("Success on cat api response->>> $data")
}) { error ->
logE("Error on cat api ->>>> $error ")
}
}
}
这里我已经成功的从Success
中获取了数据,但是我不知道如何获取error或者error中的exception。
actual fun httpClient(config: HttpClientConfig<*>.() -> Unit) = HttpClient(OkHttp) {
config(this)
install(Logging) {
logger = Logger.SIMPLE
level = LogLevel.BODY
}
expectSuccess = false
install(ContentNegotiation) {
json(Json {
prettyPrint = true
ignoreUnknownKeys = true
explicitNulls = false
})
}
}
如何在错误数据 class 中传递异常或错误代码、状态、正文?有人对此有想法吗?
对于 kotlin 挂起函数,您需要使用 try/catch 块来捕获错误:
val apiResponse = try {
ApiResponse.Success(KtorApi.getCat())
} catch (e: ClientRequestException) {
ApiResponse.Error(e.response.status)
} catch (e: Exeption) {
// some other error
ApiResponse.Error()
}
我正在学习 Ktor。我想打印错误值或异常。我从这个
ApiResponse.kt
sealed class ApiResponse<out T : Any> {
data class Success<out T : Any>(
val data: T?
) : ApiResponse<T>()
data class Error(
val responseCode: Int = -1,
) : ApiResponse<Nothing>()
fun handleResult(onSuccess: ((responseData: T?) -> Unit)?, onError: ((error: Error) -> Unit)?) {
when (this) {
is Success -> {
onSuccess?.invoke(this.data)
}
is Error -> {
onError?.invoke(this)
}
}
}
}
@Serializable
data class ErrorResponse(
var errorCode: Int = 1,
val errorMessage: String = "Something went wrong"
)
KtorApi.kt
class KtorApi(private val httpClient: HttpClient) : NetworkRoute() {
suspend fun getCat(): Response<CatResponse> {
val response = httpClient.get {
url("https://xyz/cat")
}
return apiCall(response)
}
}
CatResponse.kt
@Serializable
data class CatResponse(
val items: List<CatDetails>? = null
)
@Serializable
data class CatDetails(
val id: String? = null,
val name: String? = null,
)
ViewModel.kt
fun getCat() {
viewModelScope.launch {
KtorApi.getCat().handleResult({ data ->
logE("Success on cat api response->>> $data")
}) { error ->
logE("Error on cat api ->>>> $error ")
}
}
}
这里我已经成功的从Success
中获取了数据,但是我不知道如何获取error或者error中的exception。
actual fun httpClient(config: HttpClientConfig<*>.() -> Unit) = HttpClient(OkHttp) {
config(this)
install(Logging) {
logger = Logger.SIMPLE
level = LogLevel.BODY
}
expectSuccess = false
install(ContentNegotiation) {
json(Json {
prettyPrint = true
ignoreUnknownKeys = true
explicitNulls = false
})
}
}
如何在错误数据 class 中传递异常或错误代码、状态、正文?有人对此有想法吗?
对于 kotlin 挂起函数,您需要使用 try/catch 块来捕获错误:
val apiResponse = try {
ApiResponse.Success(KtorApi.getCat())
} catch (e: ClientRequestException) {
ApiResponse.Error(e.response.status)
} catch (e: Exeption) {
// some other error
ApiResponse.Error()
}