Skip to content

Commit cfbf6a4

Browse files
committed
Handle NoSuchKeyException in WorkspaceService and improve error handling
- Updated `WorkspaceServiceImpl` to catch `NoSuchKeyException` and throw `CsmResourceNotFoundException` with a customized error message. - Modified related exception handler to return detailed NOT_FOUND response. - Updated integration tests to validate new exception handling and error messages.
1 parent 531bec0 commit cfbf6a4

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,11 @@ open class CsmExceptionHandling : ResponseEntityExceptionHandler() {
163163
}
164164

165165
@ExceptionHandler(NoSuchKeyException::class)
166-
fun handleNoSuchKeyException(exception: NoSuchKeyException): ProblemDetail {
166+
fun handleNoSuchKeyException(): ProblemDetail {
167167
val response = ProblemDetail.forStatus(HttpStatus.NOT_FOUND)
168168
val notImplementedErrorStatus = HttpStatus.NOT_FOUND
169169
response.type = URI.create(httpStatusCodeTypePrefix + notImplementedErrorStatus.value())
170-
if (exception.awsErrorDetails().errorMessage() != null) {
171-
response.detail = exception.awsErrorDetails().errorMessage()
172-
}
170+
response.detail = "The specified file name does not exist."
173171
return response
174172
}
175173
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,21 +222,21 @@ class WorkspaceServiceIntegrationTest : CsmTestBase() {
222222
logger.info("should delete a workspace file")
223223
val resourceTestFile = resourceLoader.getResource("classpath:/$fileName").file
224224
val input = FileInputStream(resourceTestFile)
225+
val originalFilename = resourceTestFile.getName()
225226
val multipartFile =
226-
MockMultipartFile(
227-
"file", resourceTestFile.getName(), "text/plain", IOUtils.toByteArray(input))
227+
MockMultipartFile("file", originalFilename, "text/plain", IOUtils.toByteArray(input))
228228

229229
workspaceApiService.createWorkspaceFile(
230230
organizationSaved.id, workspaceSaved.id, multipartFile, true, null)
231231

232232
workspaceApiService.deleteWorkspaceFile(organizationSaved.id, workspaceSaved.id, fileName)
233233

234234
val exception =
235-
assertThrows<NoSuchKeyException> {
235+
assertThrows<CsmResourceNotFoundException> {
236236
workspaceApiService.getWorkspaceFile(organizationSaved.id, workspaceSaved.id, fileName)
237237
}
238238

239-
assertEquals("The specified key does not exist.", exception.awsErrorDetails().errorMessage())
239+
assertEquals("$originalFilename does not exist.", exception.message)
240240
}
241241

242242
@Test

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import org.springframework.web.multipart.MultipartFile
5656
import software.amazon.awssdk.awscore.exception.AwsServiceException
5757
import software.amazon.awssdk.services.s3.S3Client
5858
import software.amazon.awssdk.services.s3.model.ListObjectsV2Request
59+
import software.amazon.awssdk.services.s3.model.NoSuchKeyException
5960

6061
private const val WORKSPACE_FILES_BASE_FOLDER = "workspace-files"
6162

@@ -233,12 +234,20 @@ internal class WorkspaceServiceImpl(
233234
workspace.id,
234235
workspace.name,
235236
fileName)
236-
return InputStreamResource(
237-
s3Template
238-
.download(
239-
csmPlatformProperties.s3.bucketName,
240-
"$organizationId/$workspaceId/$WORKSPACE_FILES_BASE_FOLDER/$fileName")
241-
.inputStream)
237+
var fileResource: Resource
238+
try {
239+
fileResource =
240+
InputStreamResource(
241+
s3Template
242+
.download(
243+
csmPlatformProperties.s3.bucketName,
244+
"$organizationId/$workspaceId/$WORKSPACE_FILES_BASE_FOLDER/$fileName")
245+
.inputStream)
246+
} catch (exception: NoSuchKeyException) {
247+
throw CsmResourceNotFoundException("$fileName does not exist.", exception)
248+
}
249+
250+
return fileResource
242251
}
243252

244253
override fun createWorkspaceFile(

0 commit comments

Comments
 (0)