0
import com.google.cloud.storage.Blob;
import com.google.cloud.storage.StorageException;
import com.google.cloud.storage.StorageOptions;

public class CleanUp {

    public static void main(String... args) {

        final var storage = StorageOptions.getDefaultInstance().getService();

        var bucketName = Utilities.getBucketName();

        for (Blob blob : storage.list(bucketName).iterateAll()) {
            blob.delete(Blob.BlobSourceOption.generationMatch());
            System.out.println(blob.getName() + " deleted");
        }

        try {
            storage.delete(bucketName);
            System.out.println(bucketName + " deleted");
        }
        catch (StorageException cause) {
            System.err.println(bucketName + " does not exist");
        }
        catch (Throwable cause) {
            System.err.println(bucketName + " does not exist");
        }
    }
 }

But when I run this I get

Exception in thread "main" com.google.cloud.storage.StorageException: The specified bucket does not exist.
    at com.google.cloud.storage.spi.v1.HttpStorageRpc.translate(HttpStorageRpc.java:233)
    at com.google.cloud.storage.spi.v1.HttpStorageRpc.list(HttpStorageRpc.java:376)
    at com.google.cloud.storage.StorageImpl.lambda$listBlobs$11(StorageImpl.java:391)
    at com.google.api.gax.retrying.DirectRetryingExecutor.submit(DirectRetryingExecutor.java:105)
    at com.google.cloud.RetryHelper.run(RetryHelper.java:76)
    at com.google.cloud.RetryHelper.runWithRetries(RetryHelper.java:50)
    at com.google.cloud.storage.Retrying.run(Retrying.java:51)
    at com.google.cloud.storage.StorageImpl.listBlobs(StorageImpl.java:388)
    at com.google.cloud.storage.StorageImpl.list(StorageImpl.java:359)
    at com.forgerock.poc.bq.CleanUp.main(CleanUp.java:17)
Caused by: com.google.api.client.googleapis.json.GoogleJsonResponseException: 404 Not Found
GET https://storage.googleapis.com/storage/v1/b/eric_kolotyluk-bq-poc/o?projection=full
{
  "code" : 404,
  "errors" : [ {
    "domain" : "global",
    "message" : "The specified bucket does not exist.",
    "reason" : "notFound"
  } ],
  "message" : "The specified bucket does not exist."
}
    at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:146)
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:118)
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:37)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:428)
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1111)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:514)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:455)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:565)
    at com.google.cloud.storage.spi.v1.HttpStorageRpc.list(HttpStorageRpc.java:366)
    ... 8 more

Why can't I catch this exception/throwable? What is the GCS API doing to bypass try-catch?

I am running this code from IntelliJ, not from the command line.

Eric Kolotyluk
  • 1,958
  • 2
  • 21
  • 30

0 Answers0