Skip to content

Commit

Permalink
Merge pull request #58 from Infomaniak/add-exception
Browse files Browse the repository at this point in the history
chore: Add exceptions for null API properties and for missing objects in DB
  • Loading branch information
sirambd authored Oct 23, 2024
2 parents cbc6af9 + 12463ce commit fa14c67
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 15 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.exceptions

/**
* Thrown when we cannot find a specific property in the database
*
* @param message A message explaining what we couldn't find
*/
class NotFoundException(message: String) : Exception()
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.exceptions

/**
* Thrown when a property is null
*
* @param message The message to explain which property is null
*/
class NullPropertyException(message: String) : Exception()
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import com.infomaniak.multiplatform_swisstransfer.common.exceptions.UnknownExcep
import com.infomaniak.multiplatform_swisstransfer.common.interfaces.upload.UploadSession
import com.infomaniak.multiplatform_swisstransfer.data.NewUploadSession
import com.infomaniak.multiplatform_swisstransfer.database.controllers.UploadController
import com.infomaniak.multiplatform_swisstransfer.exceptions.NullPropertyException
import com.infomaniak.multiplatform_swisstransfer.exceptions.NotFoundException
import com.infomaniak.multiplatform_swisstransfer.network.exceptions.ApiException
import com.infomaniak.multiplatform_swisstransfer.network.exceptions.ContainerErrorsException
import com.infomaniak.multiplatform_swisstransfer.network.exceptions.NetworkException
Expand Down Expand Up @@ -112,6 +114,7 @@ class UploadManager(
* @throws NetworkException If there is a network error.
* @throws UnexpectedApiErrorFormatException If the API error format is unexpected.
* @throws UnknownException If an unknown error occurs.
* @throws NotFoundException If we cannot find the upload session in the database with the specified uuid.
*/
@Throws(
RealmException::class,
Expand All @@ -121,18 +124,20 @@ class UploadManager(
NetworkException::class,
UnexpectedApiErrorFormatException::class,
UnknownException::class,
NotFoundException::class,
)
suspend fun initUploadSession(uuid: String, recaptcha: String = ""): Unit = withContext(Dispatchers.IO) {
uploadController.getUploadByUUID(uuid)?.let { uploadSession ->
val initUploadBody = InitUploadBody(uploadSession, recaptcha)
val initUploadResponse = uploadRepository.initUpload(initUploadBody)
uploadController.updateUploadSession(
uuid = uuid,
remoteContainer = initUploadResponse.container,
remoteUploadHost = initUploadResponse.uploadHost,
remoteFilesUUID = initUploadResponse.filesUUID,
)
}
val uploadSession = uploadController.getUploadByUUID(uuid)
?: throw NotFoundException("${UploadSession::class.simpleName} not found in DB with uuid = $uuid")

val initUploadBody = InitUploadBody(uploadSession, recaptcha)
val initUploadResponse = uploadRepository.initUpload(initUploadBody)
uploadController.updateUploadSession(
uuid = uuid,
remoteContainer = initUploadResponse.container,
remoteUploadHost = initUploadResponse.uploadHost,
remoteFilesUUID = initUploadResponse.filesUUID,
)
}

/**
Expand All @@ -153,6 +158,8 @@ class UploadManager(
* @throws NetworkException If there is a network error.
* @throws UnknownException If an unknown error occurs.
* @throws RealmException If an error occurs during database access.
* @throws NotFoundException If we cannot find the upload session in the database with the specified uuid.
* @throws NullPropertyException If remoteUploadHost or remoteContainer is null.
*/
@Throws(
CancellationException::class,
Expand All @@ -161,6 +168,8 @@ class UploadManager(
NetworkException::class,
UnknownException::class,
RealmException::class,
NotFoundException::class,
NullPropertyException::class,
)
suspend fun uploadChunk(
uuid: String,
Expand All @@ -169,9 +178,10 @@ class UploadManager(
isLastChunk: Boolean,
data: ByteArray,
): Unit = withContext(Dispatchers.IO) {
val uploadSession = uploadController.getUploadByUUID(uuid) ?: return@withContext
val remoteUploadHost = uploadSession.remoteUploadHost ?: return@withContext
val remoteContainer = uploadSession.remoteContainer ?: return@withContext
val uploadSession = uploadController.getUploadByUUID(uuid)
?: throw NotFoundException("${UploadSession::class.simpleName} not found in DB with uuid = $uuid")
val remoteUploadHost = uploadSession.remoteUploadHost ?: throw NullPropertyException("Remote upload host cannot be null")
val remoteContainer = uploadSession.remoteContainer ?: throw NullPropertyException("Remote container cannot be null")

uploadRepository.uploadChunk(
uploadHost = remoteUploadHost,
Expand Down Expand Up @@ -199,6 +209,8 @@ class UploadManager(
* @throws NetworkException If there is a network error.
* @throws UnknownException If an unknown error occurs.
* @throws RealmException If an error occurs during database access.
* @throws NotFoundException If we cannot find the upload session in the database with the specified uuid.
* @throws NullPropertyException If remoteUploadHost or remoteContainer is null.
*/
@Throws(
CancellationException::class,
Expand All @@ -207,10 +219,14 @@ class UploadManager(
NetworkException::class,
UnknownException::class,
RealmException::class,
NotFoundException::class,
NullPropertyException::class,
)
suspend fun finishUploadSession(uuid: String): Unit = withContext(Dispatchers.IO) {
val uploadSession = uploadController.getUploadByUUID(uuid) ?: return@withContext
val containerUUID = uploadSession.remoteContainer?.uuid ?: return@withContext
val uploadSession = uploadController.getUploadByUUID(uuid)
?: throw NotFoundException("Unknown upload session with uuid = $uuid")
val containerUUID = uploadSession.remoteContainer?.uuid
?: throw NullPropertyException("Remote container cannot be null")

val finishUploadBody = FinishUploadBody(
containerUUID = containerUUID,
Expand Down

0 comments on commit fa14c67

Please sign in to comment.