Skip to content

Commit 14ea13a

Browse files
committed
refactor: Hide toApiError(payload) signature as best as we can and only expose toApiError()
1 parent 2736e8e commit 14ea13a

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

Legacy/src/main/java/com/infomaniak/lib/core/api/ApiController.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ object ApiController {
257257
payload: InternalErrorPayload? = null,
258258
noinline buildErrorResult: ((apiError: ApiError?, translatedErrorRes: Int) -> T)?,
259259
): T {
260-
val apiError = internalErrorCode.toApiError(payload)
260+
val apiError = createDetailedApiError(internalErrorCode, payload)
261261
val translatedError = internalErrorCode.translateRes
262262

263263
return buildErrorResult?.invoke(apiError, translatedError)
@@ -277,14 +277,18 @@ object ApiController {
277277
val bodyResponse: String? = null,
278278
)
279279

280-
fun ErrorCodeTranslated.toApiError(payload: InternalErrorPayload? = null): ApiError {
280+
fun ErrorCodeTranslated.toApiError(): ApiError = createDetailedApiError(this, null)
281+
282+
fun createDetailedApiError(errorCode: ErrorCodeTranslated, payload: InternalErrorPayload? = null): ApiError {
281283
val useKotlinxSerialization = payload?.useKotlinxSerialization
282284
return ApiError(
283-
code = code,
285+
code = errorCode.code,
284286
contextJson = if (useKotlinxSerialization == true) payload.bodyResponse?.bodyResponseToJson() else null,
285287
contextGson = when {
286288
useKotlinxSerialization == true -> null
287-
else -> runCatching { payload?.let { JsonParser.parseString(it.bodyResponse).asJsonObject } }.getOrDefault(null)
289+
else -> errorCode.runCatching {
290+
payload?.let { JsonParser.parseString(it.bodyResponse).asJsonObject }
291+
}.getOrDefault(null)
288292
},
289293
exception = payload?.exception
290294
)

0 commit comments

Comments
 (0)