Skip to content

Commit 2497bc3

Browse files
committed
Catch storage exception
1 parent 1e645dd commit 2497bc3

File tree

1 file changed

+68
-14
lines changed

1 file changed

+68
-14
lines changed

src/main/java/io/cdap/plugin/gcp/gcs/StorageClient.java

Lines changed: 68 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,14 @@ public Blob pickABlob(String path) {
7272
return null;
7373
}
7474
GCSPath gcsPath = GCSPath.from(path);
75-
Page<Blob> blobPage = storage.list(gcsPath.getBucket(), Storage.BlobListOption.prefix(gcsPath.getName()));
75+
Page<Blob> blobPage;
76+
try {
77+
blobPage = storage.list(gcsPath.getBucket(), Storage.BlobListOption.prefix(gcsPath.getName()));
78+
} catch (Exception e) {
79+
String errorReason = String.format("Unable to list objects in bucket %s.", gcsPath.getBucket());
80+
throw GCPErrorDetailsProviderUtil.getHttpResponseExceptionDetailsFromChain(e, errorReason, ErrorType.UNKNOWN,
81+
true, GCPUtils.GCS_SUPPORTED_DOC_URL);
82+
}
7683
Iterator<Blob> iterator = blobPage.getValues().iterator();
7784
while (iterator.hasNext()) {
7885
Blob blob = iterator.next();
@@ -93,7 +100,13 @@ public void setMetaData(Blob blob, Map<String, String> metaData) {
93100
if (blob == null || metaData == null || metaData.isEmpty()) {
94101
return;
95102
}
96-
storage.update(BlobInfo.newBuilder(blob.getBlobId()).setMetadata(metaData).build());
103+
try {
104+
storage.update(BlobInfo.newBuilder(blob.getBlobId()).setMetadata(metaData).build());
105+
} catch (Exception e) {
106+
String errorReason = String.format("Unable to update metadata for blob %s.", blob.getName());
107+
throw GCPErrorDetailsProviderUtil.getHttpResponseExceptionDetailsFromChain(e, errorReason, ErrorType.UNKNOWN,
108+
true, GCPUtils.GCS_SUPPORTED_DOC_URL);
109+
}
97110
}
98111

99112
/**
@@ -106,7 +119,14 @@ public void mapMetaDataForAllBlobs(String path, Consumer<Map<String, String>> fu
106119
return;
107120
}
108121
GCSPath gcsPath = GCSPath.from(path);
109-
Page<Blob> blobPage = storage.list(gcsPath.getBucket(), Storage.BlobListOption.prefix(gcsPath.getName()));
122+
Page<Blob> blobPage;
123+
try {
124+
blobPage = storage.list(gcsPath.getBucket(), Storage.BlobListOption.prefix(gcsPath.getName()));
125+
} catch (Exception e) {
126+
String errorReason = String.format("Unable to list objects in bucket %s.", gcsPath.getBucket());
127+
throw GCPErrorDetailsProviderUtil.getHttpResponseExceptionDetailsFromChain(e, errorReason, ErrorType.UNKNOWN,
128+
true, GCPUtils.GCS_SUPPORTED_DOC_URL);
129+
}
110130
Iterator<Blob> blobIterator = blobPage.iterateAll().iterator();
111131
while (blobIterator.hasNext()) {
112132
Blob blob = blobIterator.next();
@@ -179,9 +199,16 @@ public void move(GCSPath sourcePath, GCSPath destPath, boolean recursive, boolea
179199
* Get all the matching wildcard paths given the regex input.
180200
*/
181201
public List<GCSPath> getMatchedPaths(GCSPath sourcePath, boolean recursive, Pattern wildcardRegex) {
182-
Page<Blob> blobPage = storage.list(sourcePath.getBucket(), Storage.BlobListOption.prefix(
202+
Page<Blob> blobPage;
203+
try {
204+
blobPage = storage.list(sourcePath.getBucket(), Storage.BlobListOption.prefix(
183205
getWildcardPathPrefix(sourcePath, wildcardRegex)
184-
));
206+
));
207+
} catch (Exception e) {
208+
String errorReason = String.format("Unable to list objects in bucket %s.", sourcePath.getBucket());
209+
throw GCPErrorDetailsProviderUtil.getHttpResponseExceptionDetailsFromChain(e, errorReason, ErrorType.UNKNOWN,
210+
true, GCPUtils.GCS_SUPPORTED_DOC_URL);
211+
}
185212
List<String> blobPageNames = new ArrayList<>();
186213
blobPage.getValues().forEach(blob -> blobPageNames.add(blob.getName()));
187214
return getFilterMatchedPaths(sourcePath, blobPageNames, recursive);
@@ -217,8 +244,7 @@ static List<GCSPath> getFilterMatchedPaths(GCSPath sourcePath, List<String> blob
217244
*/
218245
private void pairTraverse(GCSPath sourcePath, GCSPath destPath, boolean recursive, boolean overwrite,
219246
Consumer<BlobPair> consumer) {
220-
221-
Bucket sourceBucket = null;
247+
Bucket sourceBucket;
222248
try {
223249
sourceBucket = storage.get(sourcePath.getBucket());
224250
} catch (Exception e) {
@@ -232,7 +258,7 @@ private void pairTraverse(GCSPath sourcePath, GCSPath destPath, boolean recursiv
232258
throw ErrorUtils.getProgramFailureException(new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN),
233259
errorReason, errorReason, ErrorType.USER, true, null);
234260
}
235-
Bucket destBucket = null;
261+
Bucket destBucket;
236262
try {
237263
destBucket = storage.get(destPath.getBucket());
238264
} catch (Exception e) {
@@ -250,24 +276,45 @@ private void pairTraverse(GCSPath sourcePath, GCSPath destPath, boolean recursiv
250276

251277
boolean destinationBaseExists;
252278
String baseDestName = destPath.getName();
253-
if (destPath.isBucket() || storage.get(BlobId.of(destPath.getBucket(), baseDestName)) != null) {
279+
Blob storageBlob;
280+
try {
281+
storageBlob = storage.get(BlobId.of(destPath.getBucket(), baseDestName));
282+
} catch (Exception e) {
283+
String errorReason = String.format("Unable to access GCS object '%s'.", baseDestName);
284+
throw GCPErrorDetailsProviderUtil.getHttpResponseExceptionDetailsFromChain(e, errorReason, ErrorType.UNKNOWN,
285+
true, GCPUtils.GCS_SUPPORTED_DOC_URL);
286+
}
287+
if (destPath.isBucket() || storageBlob != null) {
254288
destinationBaseExists = true;
255289
} else {
256290
// if gs://bucket2/subdir doesn't exist, check if gs://bucket2/subdir/ exists
257291
// similarly, if gs://bucket2/subdir/ doesn't exist, check if gs://bucket2/subdir exists
258292
// this is because "cp dir0 subdir" and "cp dir0 subdir/" are equivalent if the 'subdir' directory exists
259293
String modifiedName = baseDestName.endsWith("/") ?
260294
baseDestName.substring(0, baseDestName.length() - 1) : baseDestName + "/";
261-
destinationBaseExists = storage.get(BlobId.of(destPath.getBucket(), modifiedName)) != null;
295+
try {
296+
destinationBaseExists = storage.get(BlobId.of(destPath.getBucket(), modifiedName)) != null;
297+
} catch (Exception e) {
298+
String errorReason = String.format("Unable to access GCS object '%s'.", modifiedName);
299+
throw GCPErrorDetailsProviderUtil.getHttpResponseExceptionDetailsFromChain(e, errorReason, ErrorType.UNKNOWN,
300+
true, GCPUtils.GCS_SUPPORTED_DOC_URL);
301+
}
262302
}
263303

264304
List<BlobPair> copyList = new ArrayList<>();
265305
traverse(BlobId.of(sourcePath.getBucket(), sourcePath.getName()), recursive, sourceBlob -> {
266306
BlobId destBlobID = resolve(sourcePath.getName(), sourceBlob.getBlobId().getName(),
267307
destPath, destinationBaseExists);
268308
if (!overwrite) {
269-
Blob destBlob = storage.get(destBlobID);
270-
// we can't just use Blob's isDirectory() because the cloud console will create a 'directory' by creating
309+
Blob destBlob;
310+
try {
311+
destBlob = storage.get(destBlobID);
312+
} catch (Exception e) {
313+
String errorReason = String.format("Unable to access GCS object '%s'.", destBlobID.getName());
314+
throw GCPErrorDetailsProviderUtil.getHttpResponseExceptionDetailsFromChain(e, errorReason, ErrorType.UNKNOWN,
315+
true, GCPUtils.GCS_SUPPORTED_DOC_URL);
316+
}
317+
// we can't just use Blob's isDirectory() because the cloud console will create a 'directory' by creating
271318
// a 0 size placeholder blob that ends with '/'. This placeholder blob's isDirectory() method returns false,
272319
// but we don't want the overwrite check to fail on it. So we explicitly ignore the check for these 0 size
273320
// placeholder blobs.
@@ -358,8 +405,15 @@ static String append(String base, String part) {
358405
* @param consumer the blob consumer
359406
*/
360407
private void traverse(BlobId blobId, boolean recursive, Consumer<Blob> consumer) {
361-
Page<Blob> blobList = storage.list(blobId.getBucket(), Storage.BlobListOption.currentDirectory(),
362-
Storage.BlobListOption.prefix(blobId.getName()));
408+
Page<Blob> blobList;
409+
try {
410+
blobList = storage.list(blobId.getBucket(), Storage.BlobListOption.currentDirectory(),
411+
Storage.BlobListOption.prefix(blobId.getName()));
412+
} catch (Exception e) {
413+
String errorReason = String.format("");
414+
throw GCPErrorDetailsProviderUtil.getHttpResponseExceptionDetailsFromChain(e, errorReason, ErrorType.UNKNOWN,
415+
true, GCPUtils.GCS_SUPPORTED_DOC_URL);
416+
}
363417
for (Blob blob : blobList.iterateAll()) {
364418
if (!blob.isDirectory()) {
365419
consumer.accept(blob);

0 commit comments

Comments
 (0)