diff --git a/Network/Models/src/main/java/com/infomaniak/core/network/models/ApiResponse.kt b/Network/Models/src/main/java/com/infomaniak/core/network/models/ApiResponse.kt index 1286134c8..4ce29493a 100644 --- a/Network/Models/src/main/java/com/infomaniak/core/network/models/ApiResponse.kt +++ b/Network/Models/src/main/java/com/infomaniak/core/network/models/ApiResponse.kt @@ -1,6 +1,6 @@ /* * Infomaniak Core - Android - * Copyright (C) 2022-2025 Infomaniak Network SA + * Copyright (C) 2022-2026 Infomaniak Network SA * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -52,4 +52,10 @@ open class ApiResponse( var headers: ResponseHeaders = ResponseHeaders() fun isSuccess() = result == ApiResponseStatus.SUCCESS + + fun isError() = result == ApiResponseStatus.ERROR + + override fun toString(): String { + return "ApiResponse(result=$result, data=$data, uri=$uri, error=$error, responseAt=$responseAt )" + } } diff --git a/Network/src/main/kotlin/com/infomaniak/core/network/utils/apiResponse/ApiErrorException.kt b/Network/src/main/kotlin/com/infomaniak/core/network/utils/apiResponse/ApiErrorException.kt new file mode 100644 index 000000000..7c6290192 --- /dev/null +++ b/Network/src/main/kotlin/com/infomaniak/core/network/utils/apiResponse/ApiErrorException.kt @@ -0,0 +1,22 @@ +/* + * Infomaniak Core - Android + * Copyright (C) 2026 Infomaniak Network SA + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.infomaniak.core.network.utils.apiResponse + +import com.infomaniak.core.network.models.ApiResponse + +class ApiErrorException(response: ApiResponse<*>) : Exception("Error is null but required for an error $response") diff --git a/Network/src/main/kotlin/com/infomaniak/core/network/utils/apiResponse/ApiResponseException.kt b/Network/src/main/kotlin/com/infomaniak/core/network/utils/apiResponse/ApiResponseException.kt new file mode 100644 index 000000000..1fe20af3e --- /dev/null +++ b/Network/src/main/kotlin/com/infomaniak/core/network/utils/apiResponse/ApiResponseException.kt @@ -0,0 +1,22 @@ +/* + * Infomaniak Core - Android + * Copyright (C) 2026 Infomaniak Network SA + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.infomaniak.core.network.utils.apiResponse + +import com.infomaniak.core.network.models.ApiResponse + +class ApiResponseException(response: ApiResponse<*>) : Exception("Data is null but required for a success $response") diff --git a/Network/src/main/kotlin/com/infomaniak/core/network/utils/apiResponse/ApiResponseExt.kt b/Network/src/main/kotlin/com/infomaniak/core/network/utils/apiResponse/ApiResponseExt.kt new file mode 100644 index 000000000..3b52b764a --- /dev/null +++ b/Network/src/main/kotlin/com/infomaniak/core/network/utils/apiResponse/ApiResponseExt.kt @@ -0,0 +1,39 @@ +/* + * Infomaniak Core - Android + * Copyright (C) 2026 Infomaniak Network SA + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.infomaniak.core.network.utils.apiResponse + +import com.infomaniak.core.network.models.ApiError +import com.infomaniak.core.network.models.ApiResponse +import io.sentry.Sentry + +inline fun ApiResponse.onSuccess(block: T.() -> Unit): ApiResponse = apply { + if (isSuccess()) { + data?.run(block) ?: Sentry.captureException(ApiResponseException(this)) + } +} + +inline fun ApiResponse.onError(block: ApiError.() -> Unit): ApiResponse = apply { + if (isError()) { + error?.run(block) ?: Sentry.captureException(ApiErrorException(this)) + } +} + +inline fun ApiResponse.on( + onSuccess: T.() -> Unit, + onError: ApiError.() -> Unit +): ApiResponse = onSuccess(onSuccess).onError(onError)