@@ -5,35 +5,21 @@ import de.jensklingenberg.ktorfit.converter.Converter
5
5
import de.jensklingenberg.ktorfit.converter.KtorfitResult
6
6
import de.jensklingenberg.ktorfit.converter.TypeData
7
7
import io.ktor.client.call.body
8
- import io.ktor.client.plugins.ClientRequestException
9
- import io.ktor.client.plugins.RedirectResponseException
10
- import io.ktor.client.plugins.ResponseException
11
- import io.ktor.client.plugins.ServerResponseException
12
8
import io.ktor.client.statement.HttpResponse
13
- import io.ktor.client.statement.bodyAsText
14
9
import io.ktor.http.isSuccess
15
10
16
11
internal class ResultConverter (private val typeData : TypeData ) :
17
12
Converter .SuspendResponseConverter <HttpResponse , Result <Any >> {
18
13
19
- private suspend fun HttpResponse.toException (): Throwable {
20
- val exceptionResponseText = runCatching { bodyAsText() }.getOrElse { " <body failed decoding>" }
21
- return when (status.value) {
22
- in 300 .. 399 -> RedirectResponseException (this , exceptionResponseText)
23
- in 400 .. 499 -> ClientRequestException (this , exceptionResponseText)
24
- in 500 .. 599 -> ServerResponseException (this , exceptionResponseText)
25
- else -> ResponseException (this , exceptionResponseText)
26
- }
27
- }
28
-
29
14
override suspend fun convert (result : KtorfitResult ): Result <Any > {
30
15
return when (result) {
31
16
is KtorfitResult .Failure -> Result .failure(result.throwable)
32
17
is KtorfitResult .Success -> {
33
18
when {
34
19
result.response.status.isSuccess() ->
35
20
Result .success(result.response.body(typeData.typeArgs.first().typeInfo))
36
- else -> Result .failure(result.response.toException())
21
+ // we configure the client with expectSuccess
22
+ else -> error(" impossible: " + result.response)
37
23
}
38
24
}
39
25
}
0 commit comments