Skip to content

Commit b18c4e4

Browse files
committed
refactor exception handling isolate NotFound exception to the Storage service
1 parent ee145a6 commit b18c4e4

File tree

6 files changed

+32
-41
lines changed

6 files changed

+32
-41
lines changed

aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/operation/AWSS3StorageGetPresignedUrlOperation.java

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
import java.net.URL;
3131
import java.util.concurrent.ExecutorService;
3232

33-
import aws.sdk.kotlin.services.s3.model.NotFound;
34-
3533
/**
3634
* An operation to retrieve pre-signed object URL from AWS S3.
3735
*
@@ -92,19 +90,8 @@ public void start() {
9290
if (getRequest().validateObjectExistence()) {
9391
try {
9492
storageService.validateObjectExists(serviceKey);
95-
} catch (NotFound nfe) {
96-
onError.accept(new StorageException(
97-
"Unable to generate URL for non-existent path: $serviceKey",
98-
nfe,
99-
"Please ensure the path is valid or the object has been uploaded."
100-
));
101-
return;
102-
} catch (Exception exception) {
103-
onError.accept(new StorageException(
104-
"Encountered an issue while validating the existence of object",
105-
exception,
106-
"See included exception for more details and suggestions to fix."
107-
));
93+
} catch (StorageException exception) {
94+
onError.accept(exception);
10895
return;
10996
}
11097
}
@@ -116,13 +103,14 @@ public void start() {
116103
onSuccess.accept(StorageGetUrlResult.fromUrl(url));
117104
} catch (Exception exception) {
118105
onError.accept(new StorageException(
119-
"Encountered an issue while generating pre-signed URL",
120-
exception,
121-
"See included exception for more details and suggestions to fix."
106+
"Encountered an issue while generating pre-signed URL",
107+
exception,
108+
"See included exception for more details and suggestions to fix."
122109
));
123110
}
124111

125-
}, onError);
112+
},
113+
onError);
126114
}
127115
);
128116
}

aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/operation/AWSS3StoragePathGetPresignedUrlOperation.kt

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
*/
1515
package com.amplifyframework.storage.s3.operation
1616

17-
import aws.sdk.kotlin.services.s3.model.NotFound
1817
import com.amplifyframework.auth.AuthCredentialsProvider
1918
import com.amplifyframework.core.Consumer
2019
import com.amplifyframework.storage.StorageException
@@ -52,14 +51,8 @@ internal class AWSS3StoragePathGetPresignedUrlOperation(
5251
if (request.validateObjectExistence) {
5352
try {
5453
storageService.validateObjectExists(serviceKey)
55-
} catch (nfe: NotFound) {
56-
onError.accept(
57-
StorageException(
58-
"Unable to generate URL for non-existent path: $serviceKey",
59-
nfe,
60-
"Please ensure the path is valid or the object has been uploaded."
61-
)
62-
)
54+
} catch (se: StorageException) {
55+
onError.accept(se)
6356
return@submit
6457
} catch (exception: Exception) {
6558
onError.accept(

aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/service/AWSS3StorageService.kt

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ import aws.sdk.kotlin.services.s3.deleteObject
2121
import aws.sdk.kotlin.services.s3.listObjectsV2
2222
import aws.sdk.kotlin.services.s3.model.GetObjectRequest
2323
import aws.sdk.kotlin.services.s3.model.HeadObjectRequest
24+
import aws.sdk.kotlin.services.s3.model.NotFound
2425
import aws.sdk.kotlin.services.s3.paginators.listObjectsV2Paginated
2526
import aws.sdk.kotlin.services.s3.presigners.presignGetObject
2627
import aws.sdk.kotlin.services.s3.withConfig
2728
import com.amplifyframework.auth.AuthCredentialsProvider
2829
import com.amplifyframework.storage.ObjectMetadata
30+
import com.amplifyframework.storage.StorageException
2931
import com.amplifyframework.storage.StorageItem
3032
import com.amplifyframework.storage.result.StorageListResult
3133
import com.amplifyframework.storage.s3.transfer.TransferManager
@@ -92,12 +94,20 @@ internal class AWSS3StorageService(
9294
* @param serviceKey S3 service key
9395
*/
9496
override fun validateObjectExists(serviceKey: String) {
95-
runBlocking {
96-
s3Client.headObject(
97-
HeadObjectRequest {
98-
bucket = s3BucketName
99-
key = serviceKey
100-
}
97+
try {
98+
runBlocking {
99+
s3Client.headObject(
100+
HeadObjectRequest {
101+
bucket = s3BucketName
102+
key = serviceKey
103+
}
104+
)
105+
}
106+
} catch (ex: NotFound) {
107+
throw StorageException(
108+
"Unable to generate URL for non-existent path: $serviceKey",
109+
ex,
110+
"Please ensure the path is valid or the object has been uploaded"
101111
)
102112
}
103113
}

aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/service/StorageService.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import androidx.annotation.Nullable;
2121

2222
import com.amplifyframework.storage.ObjectMetadata;
23+
import com.amplifyframework.storage.StorageException;
2324
import com.amplifyframework.storage.StorageItem;
2425
import com.amplifyframework.storage.result.StorageListResult;
2526
import com.amplifyframework.storage.s3.transfer.TransferObserver;
@@ -41,8 +42,9 @@ public interface StorageService {
4142
* Throws StorageException if object is not does not exist.
4243
*
4344
* @param serviceKey key to uniquely specify item to generate URL for
45+
* @throws StorageException If object does not exist in storage
4446
*/
45-
void validateObjectExists(@NonNull String serviceKey);
47+
void validateObjectExists(@NonNull String serviceKey) throws StorageException;
4648

4749
/**
4850
* Generate pre-signed download URL for an object.

aws-storage-s3/src/test/java/com/amplifyframework/storage/s3/operation/AWSS3StorageGetPresignedUrlOperationTest.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package com.amplifyframework.storage.s3.operation
1616

1717
import android.util.Log
18-
import aws.sdk.kotlin.services.s3.model.NotFound
1918
import com.amplifyframework.auth.AuthCredentialsProvider
2019
import com.amplifyframework.core.Consumer
2120
import com.amplifyframework.storage.StorageAccessLevel
@@ -153,7 +152,7 @@ public class AWSS3StorageGetPresignedUrlOperationTest {
153152
false,
154153
true
155154
)
156-
val expectedException = NotFound({})
155+
val expectedException = StorageException("Test", "Test")
157156
storageService = mockk<StorageService>(relaxed = true)
158157
coEvery { storageService.validateObjectExists(any()) } throws expectedException
159158
coEvery { authCredentialsProvider.getIdentityId() } returns "abc"
@@ -172,7 +171,7 @@ public class AWSS3StorageGetPresignedUrlOperationTest {
172171
awsS3StorageGetPresignedUrlOperation.start()
173172

174173
// THEN
175-
verify(exactly = 1) { onError.accept(any()) }
174+
verify(exactly = 1) { onError.accept(expectedException) }
176175
verify(exactly = 0) {
177176
storageService.getPresignedUrl(any(), any(), any())
178177
}

aws-storage-s3/src/test/java/com/amplifyframework/storage/s3/operation/AWSS3StoragePathGetUrlOperationTest.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
package com.amplifyframework.storage.s3.operation
1717

18-
import aws.sdk.kotlin.services.s3.model.NotFound
1918
import com.amplifyframework.auth.AuthCredentialsProvider
2019
import com.amplifyframework.core.Consumer
2120
import com.amplifyframework.storage.StorageException
@@ -222,7 +221,7 @@ class AWSS3StoragePathGetUrlOperationTest {
222221
fun `getPresignedUrl fails with non existent S3 path when validateObjectExistence is enabled`() {
223222
// GIVEN
224223
val path = StoragePath.fromString("public/123")
225-
val expectedException = NotFound({})
224+
val expectedException = StorageException("Test", "Test")
226225
coEvery { storageService.validateObjectExists(any()) } throws expectedException
227226
val request = AWSS3StoragePathGetPresignedUrlRequest(
228227
path,
@@ -244,7 +243,7 @@ class AWSS3StoragePathGetUrlOperationTest {
244243
awsS3StorageGetPresignedUrlOperation.start()
245244

246245
// THEN
247-
verify(exactly = 1) { onError.accept(any()) }
246+
verify(exactly = 1) { onError.accept(expectedException) }
248247
verify(exactly = 0) {
249248
storageService.getPresignedUrl(any(), any(), any())
250249
}

0 commit comments

Comments
 (0)