Handle further exceptions in a less generic way

This commit is contained in:
Julian Raufelder 2021-07-06 15:30:34 +02:00
parent 210d0fa7f4
commit c23a5a2cfc
No known key found for this signature in database
GPG Key ID: 17EE71F6634E381D
7 changed files with 36 additions and 49 deletions

View File

@ -420,8 +420,7 @@ abstract class CryptoImplDecorator(
assertCryptoFileAlreadyExists(cryptoFile) assertCryptoFileAlreadyExists(cryptoFile)
} }
try { try {
data.open(context).use { stream -> data.open(context)?.use { stream ->
requireNotNull(stream)
requireNotNull(cryptoFile.size) requireNotNull(cryptoFile.size)
val encryptedTmpFile = File.createTempFile(UUID.randomUUID().toString(), ".crypto", internalCache) val encryptedTmpFile = File.createTempFile(UUID.randomUUID().toString(), ".crypto", internalCache)
try { try {
@ -444,12 +443,10 @@ abstract class CryptoImplDecorator(
return writeFromTmpFile(data, cryptoFile, encryptedTmpFile, progressAware, replace) return writeFromTmpFile(data, cryptoFile, encryptedTmpFile, progressAware, replace)
} }
} }
} catch (e: Throwable) {
throw e
} finally { } finally {
encryptedTmpFile.delete() encryptedTmpFile.delete()
} }
} } ?: throw IllegalStateException("InputStream shouldn't be null")
} catch (e: IOException) { } catch (e: IOException) {
throw FatalBackendException(e) throw FatalBackendException(e)
} }

View File

@ -481,8 +481,6 @@ open class CryptoImplVaultFormat7 : CryptoImplDecorator {
} ?: throw FatalBackendException("CloudFile size shouldn't be null") } ?: throw FatalBackendException("CloudFile size shouldn't be null")
} }
} }
} catch (e: Throwable) {
throw e
} finally { } finally {
encryptedTmpFile.delete() encryptedTmpFile.delete()
} }

View File

@ -49,7 +49,7 @@ class HttpLoggingInterceptor(private val logger: Logger, private val context: Co
private fun getResponseLoggingExceptions(request: Request, chain: Interceptor.Chain): Response { private fun getResponseLoggingExceptions(request: Request, chain: Interceptor.Chain): Response {
return try { return try {
chain.proceed(request) chain.proceed(request)
} catch (e: Exception) { } catch (e: IOException) {
logger.log("<-- HTTP FAILED: $e") logger.log("<-- HTTP FAILED: $e")
throw e throw e
} }

View File

@ -88,8 +88,6 @@ internal class S3Impl(context: Context, cloud: S3Cloud) {
return false return false
} }
throw FatalBackendException(e) throw FatalBackendException(e)
} catch (ex: Exception) {
throw handleApiError(ex, node.path)
} }
} }
@ -114,8 +112,8 @@ internal class S3Impl(context: Context, cloud: S3Cloud) {
} }
} }
} }
} catch (ex: Exception) { } catch (e: ErrorResponseException) {
throw handleApiError(ex, folder.path) throw handleApiError(e, folder.path)
} }
} }
@ -140,8 +138,8 @@ internal class S3Impl(context: Context, cloud: S3Cloud) {
.build() .build()
client().putObject(putObjectArgs) client().putObject(putObjectArgs)
} catch (ex: Exception) { } catch (e: ErrorResponseException) {
throw handleApiError(ex, folder.path) throw handleApiError(e, folder.path)
} }
return S3CloudNodeFactory.folder(parentFolder, folder.name) return S3CloudNodeFactory.folder(parentFolder, folder.name)
@ -181,8 +179,8 @@ internal class S3Impl(context: Context, cloud: S3Cloud) {
val copyObjectArgs = CopyObjectArgs.builder().bucket(cloud.s3Bucket()).`object`(targetKey).source(copySource).build() val copyObjectArgs = CopyObjectArgs.builder().bucket(cloud.s3Bucket()).`object`(targetKey).source(copySource).build()
try { try {
client().copyObject(copyObjectArgs) client().copyObject(copyObjectArgs)
} catch (ex: Exception) { } catch (e: ErrorResponseException) {
throw handleApiError(ex, source.path) throw handleApiError(e, source.path)
} }
} }
@ -191,8 +189,8 @@ internal class S3Impl(context: Context, cloud: S3Cloud) {
for (result in client().removeObjects(removeObjectsArgs)) { for (result in client().removeObjects(removeObjectsArgs)) {
try { try {
result.get() result.get()
} catch (ex: Exception) { } catch (e: ErrorResponseException) {
throw handleApiError(ex, source.path) throw handleApiError(e, source.path)
} }
} }
@ -209,8 +207,8 @@ internal class S3Impl(context: Context, cloud: S3Cloud) {
delete(source) delete(source)
val lastModified = result.headers().getDate("Last-Modified") val lastModified = result.headers().getDate("Last-Modified")
return S3CloudNodeFactory.file(target.parent, target.name, source.size, lastModified) return S3CloudNodeFactory.file(target.parent, target.name, source.size, lastModified)
} catch (ex: Exception) { } catch (e: ErrorResponseException) {
throw handleApiError(ex, source.path) throw handleApiError(e, source.path)
} }
} }
@ -257,8 +255,8 @@ internal class S3Impl(context: Context, cloud: S3Cloud) {
progressAware.onProgress(Progress.completed(UploadState.upload(file))) progressAware.onProgress(Progress.completed(UploadState.upload(file)))
return S3CloudNodeFactory.file(file.parent, file.name, size, lastModified) return S3CloudNodeFactory.file(file.parent, file.name, size, lastModified)
} catch (ex: Exception) { } catch (e: ErrorResponseException) {
throw handleApiError(ex, file.path) throw handleApiError(e, file.path)
} }
} }
} ?: throw FatalBackendException("InputStream shouldn't bee null") } ?: throw FatalBackendException("InputStream shouldn't bee null")
@ -281,8 +279,8 @@ internal class S3Impl(context: Context, cloud: S3Cloud) {
} }
}.use { out -> CopyStream.copyStreamToStream(response, out) } }.use { out -> CopyStream.copyStreamToStream(response, out) }
} }
} catch (ex: Exception) { } catch (e: ErrorResponseException) {
throw handleApiError(ex, file.path) throw handleApiError(e, file.path)
} }
progressAware.onProgress(Progress.completed(DownloadState.download(file))) progressAware.onProgress(Progress.completed(DownloadState.download(file)))
} }
@ -307,7 +305,7 @@ internal class S3Impl(context: Context, cloud: S3Cloud) {
DeleteObject(item.objectName()) DeleteObject(item.objectName())
} }
} }
} catch (e: Exception) { } catch (e: ErrorResponseException) {
throw handleApiError(e, node.path) throw handleApiError(e, node.path)
} }
@ -317,18 +315,18 @@ internal class S3Impl(context: Context, cloud: S3Cloud) {
try { try {
val error = result.get() val error = result.get()
Timber.tag("S3Impl").e("Error in deleting object " + error.objectName() + "; " + error.message()) Timber.tag("S3Impl").e("Error in deleting object " + error.objectName() + "; " + error.message())
} catch (e: Exception) { } catch (e: ErrorResponseException) {
throw handleApiError(e, node.path) throw handleApiError(e, node.path)
} }
} }
} }
@Throws(IOException::class, BackendException::class) //@Throws(IOException::class, BackendException::class)
private fun deleteFile(node: S3File) { private fun deleteFile(node: S3File) {
val removeObjectArgs = RemoveObjectArgs.builder().bucket(cloud.s3Bucket()).`object`(node.key).build() val removeObjectArgs = RemoveObjectArgs.builder().bucket(cloud.s3Bucket()).`object`(node.key).build()
try { try {
client().removeObject(removeObjectArgs) client().removeObject(removeObjectArgs)
} catch (e: Exception) { } catch (e: ErrorResponseException) {
throw handleApiError(e, "") throw handleApiError(e, "")
} }
} }
@ -340,15 +338,14 @@ internal class S3Impl(context: Context, cloud: S3Cloud) {
throw NoSuchBucketException(cloud.s3Bucket()) throw NoSuchBucketException(cloud.s3Bucket())
} }
"" ""
} catch (e: Exception) { } catch (e: ErrorResponseException) {
throw handleApiError(e, "") throw handleApiError(e, "")
} }
} }
private fun handleApiError(ex: Exception, name: String): Exception { private fun handleApiError(e: ErrorResponseException, name: String): Exception {
return if (ex is ErrorResponseException) { val errorCode = e.errorResponse().code()
val errorCode = ex.errorResponse().code() return when {
when {
isAccessProblem(errorCode) -> { isAccessProblem(errorCode) -> {
ForbiddenException() ForbiddenException()
} }
@ -359,12 +356,9 @@ internal class S3Impl(context: Context, cloud: S3Cloud) {
NoSuchCloudFileException(name) NoSuchCloudFileException(name)
} }
else -> { else -> {
FatalBackendException(ex) FatalBackendException(e)
} }
} }
} else {
FatalBackendException(ex)
}
} }
companion object { companion object {

View File

@ -16,6 +16,7 @@ import org.cryptomator.domain.WebDavCloud
import org.cryptomator.domain.exception.UnableToDecryptWebdavPasswordException import org.cryptomator.domain.exception.UnableToDecryptWebdavPasswordException
import org.cryptomator.util.SharedPreferencesHandler import org.cryptomator.util.SharedPreferencesHandler
import org.cryptomator.util.crypto.CredentialCryptor import org.cryptomator.util.crypto.CredentialCryptor
import org.cryptomator.util.crypto.FatalCryptoException
import org.cryptomator.util.file.LruFileCacheUtil import org.cryptomator.util.file.LruFileCacheUtil
import java.io.IOException import java.io.IOException
import java.nio.charset.StandardCharsets import java.nio.charset.StandardCharsets
@ -128,7 +129,7 @@ internal class WebDavCompatibleHttpClient(cloud: WebDavCloud, context: Context)
CredentialCryptor // CredentialCryptor //
.getInstance(context) // .getInstance(context) //
.decrypt(password) .decrypt(password)
} catch (e: RuntimeException) { } catch (e: FatalCryptoException) {
throw UnableToDecryptWebdavPasswordException(e) throw UnableToDecryptWebdavPasswordException(e)
} }
} }

View File

@ -45,15 +45,11 @@ class GoogleDriveClientFactory internal constructor() {
} }
}) })
} }
return try { val credential = FixedGoogleAccountCredential.usingOAuth2(context, setOf(DriveScopes.DRIVE)).also { it.setAccountName(accountName) }
val credential: FixedGoogleAccountCredential = FixedGoogleAccountCredential.usingOAuth2(context, setOf(DriveScopes.DRIVE)) return Drive.Builder(NetHttpTransport(), JacksonFactory.getDefaultInstance(), credential) //
credential.setAccountName(accountName)
Drive.Builder(NetHttpTransport(), JacksonFactory.getDefaultInstance(), credential) //
.setApplicationName("Cryptomator-Android/" + BuildConfig.VERSION_NAME) // .setApplicationName("Cryptomator-Android/" + BuildConfig.VERSION_NAME) //
.build() .build()
} catch (e: Exception) {
throw FatalBackendException(e)
}
} }
} }
} }

View File

@ -1,6 +1,7 @@
package org.cryptomator.domain.usecases.cloud package org.cryptomator.domain.usecases.cloud
import android.content.Context import android.content.Context
import org.cryptomator.domain.exception.FatalBackendException
import org.mockito.invocation.InvocationOnMock import org.mockito.invocation.InvocationOnMock
import org.mockito.kotlin.mock import org.mockito.kotlin.mock
import org.mockito.stubbing.Answer import org.mockito.stubbing.Answer
@ -30,7 +31,7 @@ internal class DataSourceCapturingAnswer<T>(private val result: T, private val a
out.write(buffer, 0, read) out.write(buffer, 0, read)
} }
} catch (e: IOException) { } catch (e: IOException) {
throw RuntimeException(e) throw FatalBackendException(e)
} }
} }