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

View File

@ -481,8 +481,6 @@ open class CryptoImplVaultFormat7 : CryptoImplDecorator {
} ?: throw FatalBackendException("CloudFile size shouldn't be null")
}
}
} catch (e: Throwable) {
throw e
} finally {
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 {
return try {
chain.proceed(request)
} catch (e: Exception) {
} catch (e: IOException) {
logger.log("<-- HTTP FAILED: $e")
throw e
}

View File

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

View File

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

View File

@ -26,7 +26,7 @@ class GoogleDriveClientFactory internal constructor() {
@Throws(FatalBackendException::class)
fun createClient(accountName: String, context: Context): Drive {
if ( SharedPreferencesHandler(context).debugMode()) {
if (SharedPreferencesHandler(context).debugMode()) {
Logger.getLogger("com.google.api.client").level = Level.CONFIG
Logger.getLogger("com.google.api.client").addHandler(object : Handler() {
override fun publish(record: LogRecord) {
@ -45,15 +45,11 @@ class GoogleDriveClientFactory internal constructor() {
}
})
}
return try {
val credential: FixedGoogleAccountCredential = FixedGoogleAccountCredential.usingOAuth2(context, setOf(DriveScopes.DRIVE))
credential.setAccountName(accountName)
Drive.Builder(NetHttpTransport(), JacksonFactory.getDefaultInstance(), credential) //
.setApplicationName("Cryptomator-Android/" + BuildConfig.VERSION_NAME) //
.build()
} catch (e: Exception) {
throw FatalBackendException(e)
}
val credential = FixedGoogleAccountCredential.usingOAuth2(context, setOf(DriveScopes.DRIVE)).also { it.setAccountName(accountName) }
return Drive.Builder(NetHttpTransport(), JacksonFactory.getDefaultInstance(), credential) //
.setApplicationName("Cryptomator-Android/" + BuildConfig.VERSION_NAME) //
.build()
}
}
}

View File

@ -1,6 +1,7 @@
package org.cryptomator.domain.usecases.cloud
import android.content.Context
import org.cryptomator.domain.exception.FatalBackendException
import org.mockito.invocation.InvocationOnMock
import org.mockito.kotlin.mock
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)
}
} catch (e: IOException) {
throw RuntimeException(e)
throw FatalBackendException(e)
}
}