Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -52,4 +52,10 @@ open class ApiResponse<T>(
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 )"
}
}
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/
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")
Comment thread
benjaminVadon marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/
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")
Comment thread
benjaminVadon marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/
package com.infomaniak.core.network.utils.apiResponse
Comment thread
benjaminVadon marked this conversation as resolved.

import com.infomaniak.core.network.models.ApiError
import com.infomaniak.core.network.models.ApiResponse
import io.sentry.Sentry

inline fun <T> ApiResponse<T>.onSuccess(block: T.() -> Unit): ApiResponse<T> = apply {
if (isSuccess()) {
data?.run(block) ?: Sentry.captureException(ApiResponseException(this))
}
Comment on lines +24 to +27
Copy link

Copilot AI Apr 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

onSuccess assumes a non-null data payload when result == SUCCESS, but the generic type parameter T is unconstrained (it can be nullable, e.g. ApiResponse<Foo?>). In that case a null success payload would be treated as an error and reported to Sentry. Consider constraining the extension to T : Any (and same for mapSuccess) or explicitly documenting/handling the nullable-success case without logging an exception.

Copilot uses AI. Check for mistakes.
}

inline fun <T> ApiResponse<T>.onError(block: ApiError.() -> Unit): ApiResponse<T> = apply {
if (isError()) {
error?.run(block) ?: Sentry.captureException(ApiErrorException(this))
}
}

inline fun <T> ApiResponse<T>.on(
onSuccess: T.() -> Unit,
onError: ApiError.() -> Unit
): ApiResponse<T> = onSuccess(onSuccess).onError(onError)
Loading