Skip to content

Commit

Permalink
Merge pull request #37 from Infomaniak/throw-realm-exceptions-as-Real…
Browse files Browse the repository at this point in the history
…mException

feat: Convert all realm exceptions to RealmException
  • Loading branch information
tevincent authored Oct 17, 2024
2 parents ede332e + ab01958 commit 3c50dcc
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Infomaniak SwissTransfer - Multiplatform
* Copyright (C) 2024 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.multiplatform_swisstransfer.common.exceptions

/**
* Thrown when an error occurs during a Realm database operation.
*
* @param cause The original cause of the exception.
*/
class RealmException(cause: Throwable) : Exception(cause)
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package com.infomaniak.multiplatform_swisstransfer

import com.infomaniak.multiplatform_swisstransfer.common.exceptions.RealmException
import com.infomaniak.multiplatform_swisstransfer.database.controllers.TransferController
import com.infomaniak.multiplatform_swisstransfer.database.controllers.UploadController
import com.infomaniak.multiplatform_swisstransfer.network.utils.SharedApiRoutes
Expand All @@ -29,16 +30,19 @@ class SharedApiUrlCreator internal constructor(
private val uploadController: UploadController,
) {

@Throws(RealmException::class)
fun downloadFilesUrl(transferUuid: String): String? {
val transfer = transferController.getTransfer(transferUuid) ?: return null
return SharedApiRoutes.downloadFiles(transfer.downloadHost, transfer.linkUuid)
}

@Throws(RealmException::class)
fun downloadFileUrl(transferUuid: String, fileUuid: String?): String? {
val transfer = transferController.getTransfer(transferUuid) ?: return null
return SharedApiRoutes.downloadFile(transfer.downloadHost, transfer.linkUuid, fileUuid)
}

@Throws(RealmException::class)
fun uploadChunkUrl(uploadUuid: String, fileUuid: String, chunkIndex: Int, isLastChunk: Boolean): String? {
val upload = uploadController.getUploadByUuid(uploadUuid) ?: return null
val containerUuid = upload.container?.uuid ?: return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
*/
package com.infomaniak.multiplatform_swisstransfer.managers

import com.infomaniak.multiplatform_swisstransfer.common.exceptions.RealmException
import com.infomaniak.multiplatform_swisstransfer.database.RealmProvider
import com.infomaniak.multiplatform_swisstransfer.database.controllers.AppSettingsController
import com.infomaniak.multiplatform_swisstransfer.database.controllers.TransferController
import com.infomaniak.multiplatform_swisstransfer.database.controllers.UploadController
import kotlin.coroutines.cancellation.CancellationException

/**
* AccountManager is responsible for orchestrating Accounts operations using Realm for local data management.
Expand All @@ -40,7 +42,7 @@ class AccountManager internal constructor(
/**
* Loads the default User account and initializes Realm Transfers for the default UserID defined in Constants.
*/
@Throws(IllegalArgumentException::class, IllegalStateException::class)
@Throws(RealmException::class, CancellationException::class)
suspend fun loadUser(userId: Int) {
appSettingsController.initAppSettings()
realmProvider.openRealmTransfers(userId)
Expand All @@ -49,7 +51,7 @@ class AccountManager internal constructor(
/**
* Delete specified User data
*/
@Throws(IllegalArgumentException::class, IllegalStateException::class)
@Throws(RealmException::class, CancellationException::class)
suspend fun removeUser(userId: Int) {

appSettingsController.removeData()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package com.infomaniak.multiplatform_swisstransfer.managers

import com.infomaniak.multiplatform_swisstransfer.common.exceptions.RealmException
import com.infomaniak.multiplatform_swisstransfer.common.interfaces.appSettings.AppSettings
import com.infomaniak.multiplatform_swisstransfer.common.models.DownloadLimit
import com.infomaniak.multiplatform_swisstransfer.common.models.EmailLanguage
Expand Down Expand Up @@ -51,10 +52,10 @@ class AppSettingsManager internal constructor(
*
* @param theme The new theme to apply.
*
* @throws IllegalArgumentException If the provided theme is invalid.
* @throws RealmException If the provided theme is invalid.
* @throws CancellationException If the operation is cancelled.
*/
@Throws(IllegalArgumentException::class, CancellationException::class)
@Throws(RealmException::class, CancellationException::class)
suspend fun setTheme(theme: Theme) = withContext(Dispatchers.IO) {
appSettingsController.setTheme(theme)
}
Expand All @@ -64,10 +65,10 @@ class AppSettingsManager internal constructor(
*
* @param validityPeriod The new validity period.
*
* @throws IllegalArgumentException If the provided validity period is invalid.
* @throws RealmException If the provided validity period is invalid.
* @throws CancellationException If the operation is cancelled.
*/
@Throws(IllegalArgumentException::class, CancellationException::class)
@Throws(RealmException::class, CancellationException::class)
suspend fun setValidityPeriod(validityPeriod: ValidityPeriod) = withContext(Dispatchers.IO) {
appSettingsController.setValidityPeriod(validityPeriod)
}
Expand All @@ -77,10 +78,10 @@ class AppSettingsManager internal constructor(
*
* @param downloadLimit The new download limit.
*
* @throws IllegalArgumentException If the provided download limit is invalid.
* @throws RealmException If the provided download limit is invalid.
* @throws CancellationException If the operation is cancelled.
*/
@Throws(IllegalArgumentException::class, CancellationException::class)
@Throws(RealmException::class, CancellationException::class)
suspend fun setDownloadLimit(downloadLimit: DownloadLimit) = withContext(Dispatchers.IO) {
appSettingsController.setDownloadLimit(downloadLimit)
}
Expand All @@ -90,10 +91,10 @@ class AppSettingsManager internal constructor(
*
* @param emailLanguage The new email language.
*
* @throws IllegalArgumentException If the provided email language is invalid.
* @throws RealmException If the provided email language is invalid.
* @throws CancellationException If the operation is cancelled.
*/
@Throws(IllegalArgumentException::class, CancellationException::class)
@Throws(RealmException::class, CancellationException::class)
suspend fun setEmailLanguage(emailLanguage: EmailLanguage) = withContext(Dispatchers.IO) {
appSettingsController.setEmailLanguage(emailLanguage)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package com.infomaniak.multiplatform_swisstransfer.managers

import com.infomaniak.multiplatform_swisstransfer.common.exceptions.RealmException
import com.infomaniak.multiplatform_swisstransfer.common.exceptions.UnknownException
import com.infomaniak.multiplatform_swisstransfer.common.interfaces.transfers.Transfer
import com.infomaniak.multiplatform_swisstransfer.common.interfaces.ui.TransferUi
Expand Down Expand Up @@ -63,6 +64,7 @@ class TransferManager internal constructor(
* @param transferDirection The direction of the transfers to retrieve (e.g., [TransferDirection.SENT] or [TransferDirection.RECEIVED]).
* @return A `Flow` that emits a list of transfers matching the specified direction.
*/
@Throws(RealmException::class)
fun getTransfers(transferDirection: TransferDirection): Flow<List<TransferUi>> {
return transferController.getTransfersFlow(transferDirection)
.map { it.mapTo(mutableListOf()) { transfer -> TransferUi(transfer) } }
Expand Down Expand Up @@ -91,6 +93,7 @@ class TransferManager internal constructor(
UnexpectedApiErrorFormatException::class,
NetworkException::class,
UnknownException::class,
RealmException::class,
)
suspend fun addTransferByUuid(transferUuid: String) = withContext(Dispatchers.IO) {
addTransfer(transferRepository.getTransferByLinkUuid(transferUuid).data, TransferDirection.SENT)
Expand Down Expand Up @@ -118,6 +121,7 @@ class TransferManager internal constructor(
UnexpectedApiErrorFormatException::class,
NetworkException::class,
UnknownException::class,
RealmException::class,
)
suspend fun addTransferByUrl(url: String) = withContext(Dispatchers.IO) {
addTransfer(transferRepository.getTransferByUrl(url).data, TransferDirection.RECEIVED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
*/
package com.infomaniak.multiplatform_swisstransfer.database.controllers

import com.infomaniak.multiplatform_swisstransfer.common.exceptions.RealmException
import com.infomaniak.multiplatform_swisstransfer.common.models.DownloadLimit
import com.infomaniak.multiplatform_swisstransfer.common.models.EmailLanguage
import com.infomaniak.multiplatform_swisstransfer.common.models.Theme
import com.infomaniak.multiplatform_swisstransfer.common.models.ValidityPeriod
import com.infomaniak.multiplatform_swisstransfer.database.RealmProvider
import com.infomaniak.multiplatform_swisstransfer.database.models.appSettings.AppSettingsDB
import com.infomaniak.multiplatform_swisstransfer.database.utils.RealmUtils.runThrowingRealm
import io.realm.kotlin.UpdatePolicy
import io.realm.kotlin.ext.query
import kotlinx.coroutines.ExperimentalCoroutinesApi
Expand All @@ -37,8 +39,8 @@ class AppSettingsController(private val realmProvider: RealmProvider) {

private val appSettingsQuery get() = realm.query<AppSettingsDB>().first()

@Throws(IllegalArgumentException::class, CancellationException::class)
suspend fun initAppSettings() {
@Throws(RealmException::class, CancellationException::class)
suspend fun initAppSettings() = runThrowingRealm {
if (appSettingsQuery.find() == null) {
realm.write {
copyToRealm(AppSettingsDB(), UpdatePolicy.ALL)
Expand All @@ -47,16 +49,16 @@ class AppSettingsController(private val realmProvider: RealmProvider) {
}

//region Get data
@Throws(IllegalArgumentException::class, CancellationException::class)
fun getAppSettingsFlow(): Flow<AppSettingsDB?> {
@Throws(RealmException::class)
fun getAppSettingsFlow(): Flow<AppSettingsDB?> = runThrowingRealm {
return appSettingsQuery.asFlow().mapLatest { it.obj }
}
//endregion

//region Update data
@Throws(IllegalArgumentException::class, CancellationException::class)
private suspend fun updateAppSettings(onUpdate: (AppSettingsDB) -> Unit) {
val appSettings = appSettingsQuery.find() ?: return
@Throws(RealmException::class, CancellationException::class)
private suspend fun updateAppSettings(onUpdate: (AppSettingsDB) -> Unit) = runThrowingRealm {
val appSettings = appSettingsQuery.find() ?: return@runThrowingRealm

realm.write {
findLatest(appSettings)?.let { mutableAppSettings ->
Expand All @@ -65,36 +67,36 @@ class AppSettingsController(private val realmProvider: RealmProvider) {
}
}

@Throws(IllegalArgumentException::class, CancellationException::class)
suspend fun setTheme(theme: Theme) {
@Throws(RealmException::class, CancellationException::class)
suspend fun setTheme(theme: Theme) = runThrowingRealm {
updateAppSettings { mutableAppSettings ->
mutableAppSettings.theme = theme
}
}

@Throws(IllegalArgumentException::class, CancellationException::class)
suspend fun setValidityPeriod(validityPeriod: ValidityPeriod) {
@Throws(RealmException::class, CancellationException::class)
suspend fun setValidityPeriod(validityPeriod: ValidityPeriod) = runThrowingRealm {
updateAppSettings { mutableAppSettings ->
mutableAppSettings.validityPeriod = validityPeriod
}
}

@Throws(IllegalArgumentException::class, CancellationException::class)
suspend fun setDownloadLimit(downloadLimit: DownloadLimit) {
@Throws(RealmException::class, CancellationException::class)
suspend fun setDownloadLimit(downloadLimit: DownloadLimit) = runThrowingRealm {
updateAppSettings { mutableAppSettings ->
mutableAppSettings.downloadLimit = downloadLimit
}
}

@Throws(IllegalArgumentException::class, CancellationException::class)
suspend fun setEmailLanguage(emailLanguage: EmailLanguage) {
@Throws(RealmException::class, CancellationException::class)
suspend fun setEmailLanguage(emailLanguage: EmailLanguage) = runThrowingRealm {
updateAppSettings { mutableAppSettings ->
mutableAppSettings.emailLanguage = emailLanguage
}
}

@Throws(IllegalArgumentException::class, CancellationException::class)
suspend fun removeData() {
@Throws(RealmException::class, CancellationException::class)
suspend fun removeData() = runThrowingRealm {
realm.write { deleteAll() }
}
//endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
*/
package com.infomaniak.multiplatform_swisstransfer.database.controllers

import com.infomaniak.multiplatform_swisstransfer.common.exceptions.RealmException
import com.infomaniak.multiplatform_swisstransfer.common.interfaces.transfers.Transfer
import com.infomaniak.multiplatform_swisstransfer.common.models.TransferDirection
import com.infomaniak.multiplatform_swisstransfer.database.RealmProvider
import com.infomaniak.multiplatform_swisstransfer.database.models.transfers.TransferDB
import com.infomaniak.multiplatform_swisstransfer.database.utils.RealmUtils.runThrowingRealm
import io.realm.kotlin.UpdatePolicy
import io.realm.kotlin.ext.query
import io.realm.kotlin.query.RealmResults
Expand All @@ -38,36 +40,38 @@ class TransferController(private val realmProvider: RealmProvider) {
private val realm by lazy { realmProvider.realmTransfers }

//region Get data
@Throws(IllegalArgumentException::class, CancellationException::class)
internal fun getTransfers(transferDirection: TransferDirection? = null): RealmResults<TransferDB>? {
@Throws(RealmException::class)
internal fun getTransfers(transferDirection: TransferDirection? = null): RealmResults<TransferDB>? = runThrowingRealm {
val sentFilterQuery = when (transferDirection) {
null -> TRUE_PREDICATE
else -> "${TransferDB.transferDirectionPropertyName} == '${transferDirection}'"
}
return realm?.query<TransferDB>(sentFilterQuery)?.sort(TransferDB::createdDateTimestamp.name, Sort.DESCENDING)?.find()
}

@Throws(IllegalArgumentException::class, CancellationException::class)
fun getTransfersFlow(transferDirection: TransferDirection): Flow<List<Transfer>> {
@Throws(RealmException::class)
fun getTransfersFlow(transferDirection: TransferDirection): Flow<List<Transfer>> = runThrowingRealm {
return getTransfers(transferDirection)?.asFlow()?.mapLatest { it.list } ?: emptyFlow()
}

fun getTransfer(linkUuid: String): Transfer? {
@Throws(RealmException::class)
fun getTransfer(linkUuid: String): Transfer? = runThrowingRealm {
return realm?.query<TransferDB>("${TransferDB::linkUuid.name} == '$linkUuid'")?.first()?.find()
}
//endregion

//region Upsert data
suspend fun upsert(transfer: Transfer, transferDirection: TransferDirection) {
@Throws(RealmException::class, CancellationException::class)
suspend fun upsert(transfer: Transfer, transferDirection: TransferDirection) = runThrowingRealm {
realm?.write {
this.copyToRealm(TransferDB(transfer, transferDirection), UpdatePolicy.ALL)
}
}
//endregion

//region Update data
@Throws(IllegalArgumentException::class, CancellationException::class)
suspend fun removeData() {
@Throws(RealmException::class, CancellationException::class)
suspend fun removeData() = runThrowingRealm {
realm?.write { deleteAll() }
}
//endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
*/
package com.infomaniak.multiplatform_swisstransfer.database.controllers

import com.infomaniak.multiplatform_swisstransfer.common.exceptions.RealmException
import com.infomaniak.multiplatform_swisstransfer.common.interfaces.upload.Upload
import com.infomaniak.multiplatform_swisstransfer.database.RealmProvider
import com.infomaniak.multiplatform_swisstransfer.database.models.upload.UploadDB
import com.infomaniak.multiplatform_swisstransfer.database.utils.RealmUtils.runThrowingRealm
import io.realm.kotlin.ext.query
import kotlin.coroutines.cancellation.CancellationException

Expand All @@ -32,30 +34,34 @@ class UploadController(private val realmProvider: RealmProvider) {
//endregion

//region Get data
@Throws(IllegalArgumentException::class, CancellationException::class)
fun getUploads(): List<Upload> = getUploadsQuery().find()
@Throws(RealmException::class)
fun getUploads(): List<Upload> = runThrowingRealm {
getUploadsQuery().find()
}

@Throws(IllegalArgumentException::class)
fun getUploadByUuid(uuid: String): Upload? {
@Throws(RealmException::class)
fun getUploadByUuid(uuid: String): Upload? = runThrowingRealm {
return realm.query<UploadDB>("${UploadDB::uuid.name} == '$uuid'").first().find()
}

@Throws(IllegalArgumentException::class, CancellationException::class)
fun getUploadsCount(): Long = getUploadsQuery().count().find()
@Throws(RealmException::class)
fun getUploadsCount(): Long = runThrowingRealm {
getUploadsQuery().count().find()
}
//endregion

//region Insert
@Throws(IllegalArgumentException::class, CancellationException::class)
suspend fun insert(upload: Upload) {
@Throws(RealmException::class, CancellationException::class)
suspend fun insert(upload: Upload) = runThrowingRealm {
realm.write {
this.copyToRealm(UploadDB(upload))
}
}
//endregion

//region Update data
@Throws(IllegalArgumentException::class, CancellationException::class)
suspend fun removeData() {
@Throws(RealmException::class, CancellationException::class)
suspend fun removeData() = runThrowingRealm {
realm.write { deleteAll() }
}
//endregion
Expand Down
Loading

0 comments on commit 3c50dcc

Please sign in to comment.