Skip to content

Commit ce2968d

Browse files
committed
#60 Catch MinIO NoSuchKey error
Signed-off-by: vityaman <vityaman.dev@yandex.ru>
1 parent f91d7f3 commit ce2968d

File tree

4 files changed

+57
-24
lines changed

4 files changed

+57
-24
lines changed

backend/people/src/main/kotlin/ru/ifmo/se/dating/people/storage/minio/MinioPictureContentStorage.kt

+43-24
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@ import io.minio.GetObjectArgs
44
import io.minio.MinioClient
55
import io.minio.PutObjectArgs
66
import io.minio.RemoveObjectArgs
7+
import io.minio.errors.ErrorResponseException
78
import kotlinx.coroutines.Dispatchers
89
import kotlinx.coroutines.withContext
910
import org.apache.http.entity.ContentType
1011
import org.springframework.beans.factory.annotation.Value
1112
import org.springframework.stereotype.Repository
13+
import ru.ifmo.se.dating.exception.NotFoundException
1214
import ru.ifmo.se.dating.people.model.Picture
1315
import ru.ifmo.se.dating.people.storage.PictureContentStorage
16+
import ru.ifmo.se.dating.people.storage.minio.exception.NoSuchKeyException
17+
import ru.ifmo.se.dating.people.storage.minio.exception.UnknownException
1418
import java.io.ByteArrayInputStream
1519

1620
@Repository
@@ -22,23 +26,22 @@ class MinioPictureContentStorage(
2226
) : PictureContentStorage {
2327
private val contentType = ContentType.IMAGE_JPEG
2428

25-
override suspend fun upload(id: Picture.Id, content: Picture.Content): Unit =
26-
withContext(Dispatchers.IO) {
27-
PutObjectArgs.builder()
28-
.bucket(bucket)
29-
.`object`(objectName(id))
30-
.stream(
31-
ByteArrayInputStream(content.bytes),
32-
content.bytes.size.toLong(),
33-
/* partSize = */ -1,
34-
)
35-
.contentType(contentType.mimeType)
36-
.build()
37-
.let { minio.putObject(it) }
38-
}
29+
override suspend fun upload(id: Picture.Id, content: Picture.Content): Unit = wrapped {
30+
PutObjectArgs.builder()
31+
.bucket(bucket)
32+
.`object`(objectName(id))
33+
.stream(
34+
ByteArrayInputStream(content.bytes),
35+
content.bytes.size.toLong(),
36+
/* partSize = */ -1,
37+
)
38+
.contentType(contentType.mimeType)
39+
.build()
40+
.let { minio.putObject(it) }
41+
}
3942

40-
override suspend fun download(id: Picture.Id): Picture.Content =
41-
withContext(Dispatchers.IO) {
43+
override suspend fun download(id: Picture.Id): Picture.Content = try {
44+
wrapped {
4245
GetObjectArgs.builder()
4346
.bucket(bucket)
4447
.`object`(objectName(id))
@@ -47,15 +50,31 @@ class MinioPictureContentStorage(
4750
.use { it.readBytes() }
4851
.let { Picture.Content(it) }
4952
}
53+
} catch (e: NoSuchKeyException) {
54+
throw NotFoundException(e.message!!, e)
55+
}
5056

51-
override suspend fun remove(id: Picture.Id) =
52-
withContext(Dispatchers.IO) {
53-
RemoveObjectArgs.builder()
54-
.bucket(bucket)
55-
.`object`(objectName(id))
56-
.build()
57-
.let { minio.removeObject(it) }
58-
}
57+
override suspend fun remove(id: Picture.Id) = wrapped {
58+
RemoveObjectArgs.builder()
59+
.bucket(bucket)
60+
.`object`(objectName(id))
61+
.build()
62+
.let { minio.removeObject(it) }
63+
}
5964

6065
private fun objectName(id: Picture.Id) = "$id.jpg"
66+
67+
private suspend fun <T> wrapped(action: () -> T) = try {
68+
withContext(Dispatchers.IO) { action() }
69+
} catch (e: ErrorResponseException) {
70+
val bucketName: String? = e.errorResponse().bucketName()
71+
val objectName: String? = e.errorResponse().objectName()
72+
when (e.errorResponse().code()) {
73+
"NoSuchKey" ->
74+
throw NoSuchKeyException("Key (bucket: $bucketName, object: $objectName) not found")
75+
76+
else ->
77+
throw UnknownException("Unknown error: ${e.errorResponse().message()}", e)
78+
}
79+
}
6180
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package ru.ifmo.se.dating.people.storage.minio.exception
2+
3+
import ru.ifmo.se.dating.exception.GenericException
4+
5+
sealed class MinioException(message: String, cause: Throwable? = null) :
6+
GenericException(message, cause)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package ru.ifmo.se.dating.people.storage.minio.exception
2+
3+
class NoSuchKeyException(message: String, cause: Throwable? = null) :
4+
MinioException(message, cause)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package ru.ifmo.se.dating.people.storage.minio.exception
2+
3+
class UnknownException(message: String, cause: Throwable? = null) :
4+
MinioException(message, cause)

0 commit comments

Comments
 (0)