From f93a9ac2f6cd8e4e709612fcee62090ba08d2019 Mon Sep 17 00:00:00 2001 From: Sebastian Schuberth Date: Thu, 19 Oct 2023 10:55:36 +0200 Subject: [PATCH] refactor(S3FileStorage): Consistenly use `runCatching` Avoid to suppress exceptions by consistently using `runCatching`. Trivially reduce nesting and improve a log message while at it. Signed-off-by: Sebastian Schuberth --- .../src/main/kotlin/storage/S3FileStorage.kt | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/utils/ort/src/main/kotlin/storage/S3FileStorage.kt b/utils/ort/src/main/kotlin/storage/S3FileStorage.kt index 54b77f069042..4af5f5824cc3 100644 --- a/utils/ort/src/main/kotlin/storage/S3FileStorage.kt +++ b/utils/ort/src/main/kotlin/storage/S3FileStorage.kt @@ -45,7 +45,6 @@ import software.amazon.awssdk.services.s3.model.S3Exception * blocking, but the [write] operation is asynchronous unless a [customEndpoint] is provided. Contents are compressed * before store if the [compression] flag is set to true. */ -@Suppress("SwallowedException") class S3FileStorage( /** The AWS access key */ private val accessKeyId: String? = null, @@ -106,13 +105,13 @@ class S3FileStorage( bucket(bucketName) }.build() - return try { + return runCatching { val response = s3Client.getObjectAsBytes(request) val stream = ByteArrayInputStream(response.asByteArray()) if (compression) XZCompressorInputStream(stream) else stream - } catch (e: NoSuchKeyException) { - throw NoSuchFileException(File(path)) - } + }.onFailure { exception -> + if (exception is NoSuchKeyException) throw NoSuchFileException(File(path)) + }.getOrThrow() } override fun write(path: String, inputStream: InputStream) { @@ -121,7 +120,7 @@ class S3FileStorage( bucket(bucketName) }.build() - inputStream.use { + val body = inputStream.use { if (compression) { val stream = ByteArrayOutputStream() XZCompressorOutputStream(stream).write(it.readBytes()) @@ -129,12 +128,12 @@ class S3FileStorage( } else { RequestBody.fromBytes(it.readBytes()) } - }.let { - try { - s3Client.putObject(request, it) - } catch (e: S3Exception) { - logger.warn { "Can not write $path to S3 bucket $bucketName" } - } + } + + runCatching { + s3Client.putObject(request, body) + }.onFailure { exception -> + if (exception is S3Exception) logger.warn { "Can not write '$path' to S3 bucket '$bucketName'." } } } }