2828import static com .google .gcloud .spi .StorageRpc .Option .IF_SOURCE_GENERATION_NOT_MATCH ;
2929import static com .google .gcloud .spi .StorageRpc .Option .IF_SOURCE_METAGENERATION_MATCH ;
3030import static com .google .gcloud .spi .StorageRpc .Option .IF_SOURCE_METAGENERATION_NOT_MATCH ;
31+ import static java .net .HttpURLConnection .HTTP_NOT_FOUND ;
3132import static java .nio .charset .StandardCharsets .UTF_8 ;
3233
3334import com .google .api .services .storage .model .StorageObject ;
@@ -176,7 +177,14 @@ public BucketInfo get(String bucket, BucketGetOption... options) {
176177 new Callable <com .google .api .services .storage .model .Bucket >() {
177178 @ Override
178179 public com .google .api .services .storage .model .Bucket call () {
179- return storageRpc .get (bucketPb , optionsMap );
180+ try {
181+ return storageRpc .get (bucketPb , optionsMap );
182+ } catch (StorageException ex ) {
183+ if (ex .code () == HTTP_NOT_FOUND ) {
184+ return null ;
185+ }
186+ throw ex ;
187+ }
180188 }
181189 }, options ().retryParams (), EXCEPTION_HANDLER );
182190 return answer == null ? null : BucketInfo .fromPb (answer );
@@ -198,7 +206,14 @@ public BlobInfo get(BlobId blob, BlobGetOption... options) {
198206 StorageObject storageObject = runWithRetries (new Callable <StorageObject >() {
199207 @ Override
200208 public StorageObject call () {
201- return storageRpc .get (storedObject , optionsMap );
209+ try {
210+ return storageRpc .get (storedObject , optionsMap );
211+ } catch (StorageException ex ) {
212+ if (ex .code () == HTTP_NOT_FOUND ) {
213+ return null ;
214+ }
215+ throw ex ;
216+ }
202217 }
203218 }, options ().retryParams (), EXCEPTION_HANDLER );
204219 return storageObject == null ? null : BlobInfo .fromPb (storageObject );
@@ -510,23 +525,28 @@ public BatchResponse apply(BatchRequest batchRequest) {
510525 List <BatchResponse .Result <BlobInfo >> updates = transformBatchResult (
511526 toUpdate , response .updates , BlobInfo .FROM_PB_FUNCTION );
512527 List <BatchResponse .Result <BlobInfo >> gets = transformBatchResult (
513- toGet , response .gets , BlobInfo .FROM_PB_FUNCTION );
528+ toGet , response .gets , BlobInfo .FROM_PB_FUNCTION , HTTP_NOT_FOUND );
514529 return new BatchResponse (deletes , updates , gets );
515530 }
516531
517532 private <I , O extends Serializable > List <BatchResponse .Result <O >> transformBatchResult (
518533 Iterable <Tuple <StorageObject , Map <StorageRpc .Option , ?>>> request ,
519- Map <StorageObject , Tuple <I , StorageException >> results , Function <I , O > transform ) {
534+ Map <StorageObject , Tuple <I , StorageException >> results , Function <I , O > transform ,
535+ int ... nullOnErrorCodes ) {
536+ Set nullOnErrorCodesSet = Sets .newHashSet (Ints .asList (nullOnErrorCodes ));
520537 List <BatchResponse .Result <O >> response = Lists .newArrayListWithCapacity (results .size ());
521538 for (Tuple <StorageObject , ?> tuple : request ) {
522539 Tuple <I , StorageException > result = results .get (tuple .x ());
523- I object = result .x ();
524- StorageException exception = result .y ();
525- if (exception != null ) {
526- response .add (new BatchResponse .Result <O >(exception ));
540+ if (result .x () != null ) {
541+ response .add (BatchResponse .Result .of (transform .apply (result .x ())));
527542 } else {
528- response .add (object != null ?
529- BatchResponse .Result .of (transform .apply (object )) : BatchResponse .Result .<O >empty ());
543+ StorageException exception = result .y ();
544+ if (nullOnErrorCodesSet .contains (exception .code ())) {
545+ //noinspection unchecked
546+ response .add (BatchResponse .Result .<O >empty ());
547+ } else {
548+ response .add (new BatchResponse .Result <O >(exception ));
549+ }
530550 }
531551 }
532552 return response ;
0 commit comments