-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve converter error handling #389
- Loading branch information
Showing
24 changed files
with
300 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Migration | ||
Here is how to migrate from deprecated code: | ||
|
||
## From 1.7.0 to 1.8.0 | ||
|
||
### SuspendResponseConverter | ||
Implement **override suspend fun convert(result: KtorfitResult)** | ||
|
||
```kotlin | ||
public suspend fun convert(result: KtorfitResult): T { | ||
return when (result) { | ||
is KtorfitResult.Failure -> { | ||
throw result.throwable // Or do something with the throwable | ||
} | ||
|
||
is KtorfitResult.Success -> { | ||
val response = result.response | ||
//Put the code that was in your other convert function here | ||
} | ||
} | ||
} | ||
``` | ||
|
||
Redirect the deprecated function to the new function: | ||
|
||
```kotlin | ||
override suspend fun convert(response: HttpResponse): Any { | ||
return convert(KtorfitResult.Success(response)) | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
...it-lib-common/src/commonMain/kotlin/de/jensklingenberg/ktorfit/converter/KtorfitResult.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package de.jensklingenberg.ktorfit.converter | ||
|
||
import io.ktor.client.statement.* | ||
|
||
/** | ||
* Represents the result from a Ktorfit request. */ | ||
public sealed interface KtorfitResult { | ||
/** | ||
* Represents a successful response. | ||
* @property response The HTTP response. | ||
*/ | ||
public class Success(public val response: HttpResponse) : KtorfitResult | ||
|
||
/** | ||
* Represents a failed response. | ||
* @property throwable The throwable associated with the failure. | ||
*/ | ||
public class Failure(public val throwable: Throwable) : KtorfitResult | ||
} |
49 changes: 49 additions & 0 deletions
49
...tlin/de/jensklingenberg/ktorfit/converter/builtin/DefaultResponseClassSuspendConverter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package de.jensklingenberg.ktorfit.converter.builtin | ||
|
||
import de.jensklingenberg.ktorfit.Ktorfit | ||
import de.jensklingenberg.ktorfit.Response | ||
import de.jensklingenberg.ktorfit.converter.Converter | ||
import de.jensklingenberg.ktorfit.converter.KtorfitResult | ||
import de.jensklingenberg.ktorfit.internal.TypeData | ||
import io.ktor.client.call.* | ||
import io.ktor.client.statement.* | ||
|
||
internal class DefaultResponseClassSuspendConverter(private val typeData: TypeData, private val ktorfit: Ktorfit) : | ||
Converter.SuspendResponseConverter<HttpResponse, Response<Any?>> { | ||
override suspend fun convert(response: HttpResponse): Response<Any?> { | ||
return convert(KtorfitResult.Success(response)) | ||
} | ||
|
||
override suspend fun convert(result: KtorfitResult): Response<Any?> { | ||
return when (result) { | ||
is KtorfitResult.Success -> { | ||
val typeInfo = typeData.typeArgs.first().typeInfo | ||
val rawResponse = result.response | ||
val code: Int = rawResponse.status.value | ||
when { | ||
code < 200 || code >= 300 -> { | ||
val errorBody = rawResponse.body<Any>() | ||
Response.error(errorBody, rawResponse) | ||
} | ||
|
||
code == 204 || code == 205 -> { | ||
Response.success(null, rawResponse) | ||
} | ||
|
||
else -> { | ||
val convertedBody = ktorfit.nextSuspendResponseConverter( | ||
null, | ||
typeData.typeArgs.first() | ||
)?.convert(rawResponse) | ||
?: rawResponse.body(typeInfo) | ||
Response.success(convertedBody, rawResponse) | ||
} | ||
} | ||
} | ||
|
||
is KtorfitResult.Failure -> { | ||
throw result.throwable | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.