Skip to content

Commit 5b0c14e

Browse files
committed
Manage S3Exception in exception handling
1 parent 77de226 commit 5b0c14e

File tree

4 files changed

+35
-13
lines changed

4 files changed

+35
-13
lines changed

api/src/integrationTest/kotlin/com/cosmotech/api/home/workspace/WorkspaceControllerTests.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,7 @@ class WorkspaceControllerTests : ControllerTestBase() {
692692
.param("file_name", "Wrong file name")
693693
.accept(MediaType.APPLICATION_OCTET_STREAM))
694694
.andExpect(status().is4xxClientError)
695-
.andExpect(jsonPath("$.detail").value("The specified key does not exist."))
695+
.andExpect(jsonPath("$.detail").value("Wrong file name does not exist."))
696696
.andDo(MockMvcResultHandlers.print())
697697
}
698698
}

common/src/main/kotlin/com/cosmotech/common/exceptions/CsmExceptionHandling.kt

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT license.
33
package com.cosmotech.common.exceptions
44

5+
import io.awspring.cloud.s3.S3Exception
56
import java.net.URI
67
import org.apache.commons.lang3.NotImplementedException
78
import org.springframework.core.Ordered
@@ -23,6 +24,7 @@ import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExcep
2324
import org.springframework.web.util.BindErrorUtils
2425
import software.amazon.awssdk.services.s3.model.NoSuchKeyException
2526

27+
@Suppress("TooManyFunctions")
2628
@Order(Ordered.HIGHEST_PRECEDENCE)
2729
@RestControllerAdvice
2830
open class CsmExceptionHandling : ResponseEntityExceptionHandler() {
@@ -165,9 +167,20 @@ open class CsmExceptionHandling : ResponseEntityExceptionHandler() {
165167
@ExceptionHandler(NoSuchKeyException::class)
166168
fun handleNoSuchKeyException(): ProblemDetail {
167169
val response = ProblemDetail.forStatus(HttpStatus.NOT_FOUND)
168-
val notImplementedErrorStatus = HttpStatus.NOT_FOUND
169-
response.type = URI.create(httpStatusCodeTypePrefix + notImplementedErrorStatus.value())
170+
val noSuchKeyErrorStatus = HttpStatus.NOT_FOUND
171+
response.type = URI.create(httpStatusCodeTypePrefix + noSuchKeyErrorStatus.value())
170172
response.detail = "The specified file name does not exist."
171173
return response
172174
}
175+
176+
@ExceptionHandler(S3Exception::class)
177+
fun handleS3Exception(exception: S3Exception): ProblemDetail {
178+
val response = ProblemDetail.forStatus(HttpStatus.INTERNAL_SERVER_ERROR)
179+
val internalServerErrorStatus = HttpStatus.INTERNAL_SERVER_ERROR
180+
response.type = URI.create(httpStatusCodeTypePrefix + internalServerErrorStatus.value())
181+
if (exception.message != null) {
182+
response.detail = exception.message
183+
}
184+
return response
185+
}
173186
}

workspace/src/integrationTest/kotlin/com/cosmotech/workspace/service/WorkspaceServiceIntegrationTest.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ import org.springframework.mock.web.MockMultipartFile
4848
import org.springframework.test.context.ActiveProfiles
4949
import org.springframework.test.context.junit.jupiter.SpringExtension
5050
import org.springframework.test.context.junit4.SpringRunner
51-
import software.amazon.awssdk.services.s3.model.NoSuchKeyException
5251

5352
@ActiveProfiles(profiles = ["workspace-test"])
5453
@ExtendWith(MockKExtension::class)
@@ -188,11 +187,11 @@ class WorkspaceServiceIntegrationTest : CsmTestBase() {
188187
organizationSaved.id, workspaceSaved.id, multipartFile, true, null)
189188

190189
val exception =
191-
assertThrows<NoSuchKeyException> {
190+
assertThrows<CsmResourceNotFoundException> {
192191
workspaceApiService.getWorkspaceFile(organizationSaved.id, workspaceSaved.id, "WrongName")
193192
}
194193

195-
assertEquals("The specified key does not exist.", exception.awsErrorDetails().errorMessage())
194+
assertEquals("WrongName does not exist.", exception.message)
196195
}
197196

198197
@Test

workspace/src/main/kotlin/com/cosmotech/workspace/service/WorkspaceServiceImpl.kt

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -335,13 +335,23 @@ internal class WorkspaceServiceImpl(
335335
.prefix(prefix)
336336
.build()
337337

338-
return s3Client
339-
.listObjectsV2Paginator(listObjectsRequest)
340-
.stream()
341-
.flatMap {
342-
it.contents().stream().map { WorkspaceFile(fileName = it.key().removePrefix(prefix)) }
343-
}
344-
.toList()
338+
try {
339+
return s3Client
340+
.listObjectsV2Paginator(listObjectsRequest)
341+
.stream()
342+
.flatMap { s3Object ->
343+
s3Object.contents().stream().map {
344+
WorkspaceFile(fileName = it.key().removePrefix(prefix))
345+
}
346+
}
347+
.toList()
348+
} catch (e: AwsServiceException) {
349+
throw S3Exception("Something wrong happened when listing workspace files", e)
350+
} catch (e: SdkClientException) {
351+
throw S3Exception("Something wrong happened when listing workspace files", e)
352+
} catch (e: S3Exception) {
353+
throw S3Exception("Something wrong happened when listing workspace files", e)
354+
}
345355
}
346356

347357
private fun deleteS3WorkspaceObject(

0 commit comments

Comments
 (0)