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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The name ApiErrorException doesn't really reflect what it's always about (according to the exception message). Same for ApiResponseException.
Nothing in the class name tells you that it's about missing data.

I have multiple suggestions:

  1. Merge these 2 exceptions into one, with the name of any of them, and error message as a constructor parameter
  2. Create a sealed class hierarchy with the name ApiResponseException for the base class, and 2 nested subclasses named something like MissingErrorField and MissingDataField.

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 : Any> ApiResponse<T>.onSuccess(block: T.() -> Unit): ApiResponse<T> = apply {
if (isSuccess()) {
data?.run(block) ?: Sentry.captureException(ApiResponseException(this))
}
}

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

inline fun <T : Any> ApiResponse<T>.on(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Will we really need this function?

I feel like its usages would not look idiomatic, and a bit redundant with repetition of the word on:

someResponse.on(
    onSuccess = { whatever() },
    onFailure = { somethingElse() }

I think onSuccess and onError are good enough for our use cases.

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