Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: When ApiError the context field throws an exception #193

Merged
merged 4 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/main/java/com/infomaniak/lib/core/api/ApiController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ object ApiController {
return when {
response.code >= 500 -> {
createErrorResponse(
apiError = ApiError(context = bodyResponse.bodyResponseToJson(), exception = ServerErrorException()),
apiError = createApiError(useKotlinxSerialization, bodyResponse, ServerErrorException()),
translatedError = R.string.serverError,
buildErrorResult = buildErrorResult,
)
Expand Down Expand Up @@ -208,7 +208,7 @@ object ApiController {
} else {
createErrorResponse(
translatedError = R.string.anErrorHasOccurred,
apiError = ApiError(context = bodyResponse.bodyResponseToJson(), exception = exception),
apiError = createApiError(useKotlinxSerialization, bodyResponse, exception = exception),
buildErrorResult = buildErrorResult,
)
}
Expand All @@ -229,6 +229,12 @@ object ApiController {
buildErrorResult = buildErrorResult,
)

fun createApiError(useKotlinxSerialization: Boolean, bodyResponse: String, exception: Exception) = ApiError(
contextJson = if (useKotlinxSerialization) bodyResponse.bodyResponseToJson() else null,
contextGson = if (useKotlinxSerialization) JsonParser.parseString(bodyResponse).asJsonObject else null,
exception = exception
)

inline fun <reified T> createErrorResponse(
@StringRes translatedError: Int,
apiError: ApiError? = null,
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/com/infomaniak/lib/core/models/ApiError.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,26 @@
*/
package com.infomaniak.lib.core.models

import com.google.gson.annotations.SerializedName
import kotlinx.serialization.Contextual
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonObject
import com.google.gson.JsonObject as GsonObject

@Serializable
class ApiError(
val code: String? = null,
val description: String? = null,
@Contextual
val context: JsonObject? = null,
@SerializedName("context")
var contextGson: GsonObject? = null,
@Contextual
@SerialName("context")
var contextJson: JsonObject? = null,
val errors: Array<ApiError>? = null,
@Contextual
val exception: Exception? = null
)
) {

}
sirambd marked this conversation as resolved.
Show resolved Hide resolved
Loading