1
1
/*
2
2
* Infomaniak Core - Android
3
- * Copyright (C) 2022-2024 Infomaniak Network SA
3
+ * Copyright (C) 2022-2025 Infomaniak Network SA
4
4
*
5
5
* This program is free software: you can redistribute it and/or modify
6
6
* it under the terms of the GNU General Public License as published by
17
17
*/
18
18
package com.infomaniak.lib.core.api
19
19
20
- import androidx.annotation.StringRes
21
20
import com.google.gson.Gson
22
21
import com.google.gson.GsonBuilder
23
22
import com.google.gson.JsonParser
24
23
import com.google.gson.reflect.TypeToken
25
24
import com.infomaniak.lib.core.BuildConfig.LOGIN_ENDPOINT_URL
26
25
import com.infomaniak.lib.core.InfomaniakCore
27
- import com.infomaniak.lib.core.R
28
26
import com.infomaniak.lib.core.auth.TokenInterceptorListener
29
27
import com.infomaniak.lib.core.models.ApiError
30
28
import com.infomaniak.lib.core.models.ApiResponse
31
29
import com.infomaniak.lib.core.models.ApiResponseStatus.ERROR
32
30
import com.infomaniak.lib.core.networking.HttpClient
33
31
import com.infomaniak.lib.core.networking.HttpUtils
34
32
import com.infomaniak.lib.core.utils.CustomDateTypeAdapter
33
+ import com.infomaniak.lib.core.utils.ErrorCodeTranslated
35
34
import com.infomaniak.lib.core.utils.isNetworkException
36
35
import com.infomaniak.lib.core.utils.isSerializationException
37
36
import com.infomaniak.lib.login.ApiToken
@@ -191,8 +190,8 @@ object ApiController {
191
190
scope.setExtra(" bodyResponse" , bodyResponse)
192
191
}
193
192
createErrorResponse(
194
- apiError = createApiError(useKotlinxSerialization, bodyResponse, ServerErrorException (bodyResponse)) ,
195
- translatedError = R .string.serverError ,
193
+ InternalTranslatedErrorCode . UnknownError ,
194
+ InternalErrorPayload ( ServerErrorException (bodyResponse), useKotlinxSerialization, bodyResponse) ,
196
195
buildErrorResult = buildErrorResult,
197
196
)
198
197
}
@@ -202,7 +201,7 @@ object ApiController {
202
201
}
203
202
} catch (refreshTokenException: RefreshTokenException ) {
204
203
refreshTokenException.printStackTrace()
205
- return createErrorResponse(translatedError = R .string.anErrorHasOccurred , buildErrorResult = buildErrorResult)
204
+ return createErrorResponse(InternalTranslatedErrorCode . UnknownError , buildErrorResult = buildErrorResult)
206
205
} catch (exception: Exception ) {
207
206
exception.printStackTrace()
208
207
@@ -217,8 +216,8 @@ object ApiController {
217
216
}
218
217
219
218
createErrorResponse(
220
- translatedError = R .string.anErrorHasOccurred ,
221
- apiError = createApiError(useKotlinxSerialization, bodyResponse, exception = exception ),
219
+ InternalTranslatedErrorCode . UnknownError ,
220
+ InternalErrorPayload (exception, useKotlinxSerialization, bodyResponse ),
222
221
buildErrorResult = buildErrorResult,
223
222
)
224
223
}
@@ -232,7 +231,9 @@ object ApiController {
232
231
}
233
232
234
233
if (apiResponse is ApiResponse <* > && apiResponse.result == ERROR ) {
235
- apiResponse.translatedError = R .string.anErrorHasOccurred
234
+ @Suppress(" DEPRECATION" )
235
+ apiResponse.translatedError = InternalTranslatedErrorCode .UnknownError .translateRes
236
+ apiResponse.error = InternalTranslatedErrorCode .UnknownError .toApiError()
236
237
}
237
238
238
239
return apiResponse
@@ -245,26 +246,20 @@ object ApiController {
245
246
inline fun <reified T > createInternetErrorResponse (
246
247
noNetwork : Boolean = false,
247
248
noinline buildErrorResult : ((apiError: ApiError ? , translatedErrorRes: Int ) -> T )? ,
248
- ) = createErrorResponse<T >(
249
- translatedError = if (noNetwork) R .string.noConnection else R .string.connectionError ,
250
- apiError = ApiError (exception = NetworkException ()),
249
+ ): T = createErrorResponse<T >(
250
+ if (noNetwork) InternalTranslatedErrorCode . NoConnection else InternalTranslatedErrorCode . ConnectionError ,
251
+ InternalErrorPayload ( NetworkException ()),
251
252
buildErrorResult = buildErrorResult,
252
253
)
253
254
254
- fun createApiError (useKotlinxSerialization : Boolean , bodyResponse : String , exception : Exception ) = ApiError (
255
- contextJson = if (useKotlinxSerialization) bodyResponse.bodyResponseToJson() else null ,
256
- contextGson = when {
257
- useKotlinxSerialization -> null
258
- else -> runCatching { JsonParser .parseString(bodyResponse).asJsonObject }.getOrDefault(null )
259
- },
260
- exception = exception
261
- )
262
-
263
255
inline fun <reified T > createErrorResponse (
264
- @StringRes translatedError : Int ,
265
- apiError : ApiError ? = null,
256
+ internalErrorCode : InternalTranslatedErrorCode ,
257
+ payload : InternalErrorPayload ? = null,
266
258
noinline buildErrorResult : ((apiError: ApiError ? , translatedErrorRes: Int ) -> T )? ,
267
259
): T {
260
+ val apiError = createDetailedApiError(internalErrorCode, payload)
261
+ val translatedError = internalErrorCode.translateRes
262
+
268
263
return buildErrorResult?.invoke(apiError, translatedError)
269
264
? : ApiResponse <Any >(result = ERROR , error = apiError, translatedError = translatedError) as T
270
265
}
@@ -275,4 +270,27 @@ object ApiController {
275
270
enum class ApiMethod {
276
271
GET , PUT , POST , DELETE , PATCH
277
272
}
273
+
274
+ data class InternalErrorPayload (
275
+ val exception : Exception ? = null ,
276
+ val useKotlinxSerialization : Boolean? = null ,
277
+ val bodyResponse : String? = null ,
278
+ )
279
+
280
+ fun ErrorCodeTranslated.toApiError (): ApiError = createDetailedApiError(this , null )
281
+
282
+ fun createDetailedApiError (errorCode : ErrorCodeTranslated , payload : InternalErrorPayload ? = null): ApiError {
283
+ val useKotlinxSerialization = payload?.useKotlinxSerialization
284
+ return ApiError (
285
+ code = errorCode.code,
286
+ contextJson = if (useKotlinxSerialization == true ) payload.bodyResponse?.bodyResponseToJson() else null ,
287
+ contextGson = when {
288
+ useKotlinxSerialization == true -> null
289
+ else -> errorCode.runCatching {
290
+ payload?.let { JsonParser .parseString(it.bodyResponse).asJsonObject }
291
+ }.getOrDefault(null )
292
+ },
293
+ exception = payload?.exception
294
+ )
295
+ }
278
296
}
0 commit comments