google-cloud-go icon indicating copy to clipboard operation
google-cloud-go copied to clipboard

storage: ErrObjectNotExist isn't returning while using CopierFrom.

Open nalkhovikleverx opened this issue 1 year ago • 1 comments

Client

golang google cloud storage

Environment

Alpine Docker on GKE go version go1.22.6 linux/amd64

Code and Dependencies

func (c *gcs) CopyObject(ctx context.Context, cmd CopyObjectCommand) (*storage.ObjectAttrs, error) {
	bucket := c.client.Bucket(cmd.Bucket)

	objAttr, err := bucket.Object(cmd.DestinationPath).CopierFrom(bucket.Object(cmd.SourcePath)).Run(ctx)
	if err != nil {
		if errors.Is(err, storage.ErrObjectNotExist) {
			return nil, application.ErrObjectNotExist
		}
	}

Client created like this:

	storageClient, err := storage.NewClient(ctx, option.WithTelemetryDisabled())
	if err != nil {
		return fmt.Errorf("failed to create storage client: %w", err)
	}
	storageClient.SetRetry(
		storage.WithBackoff(gax.Backoff{
			Initial:    cfg.StorageClient.InitialRetryDelay,
			Max:        cfg.StorageClient.MaxRetryDelay,
			Multiplier: cfg.StorageClient.RetryBackoffMultiplier,
		}),
		storage.WithPolicy(storage.RetryAlways),
	)
}
go.mod
module mymodule

go 1.22

require (
	cloud.google.com/go/storage v1.43.0
)

Expected behavior

When I call this function with not existed file I expect that the storage.ErrObjectNotExist returned.

Actual behavior

When I call this function with not existed file, I got the *googleapi.Error instance.

nalkhovikleverx avatar Aug 23 '24 18:08 nalkhovikleverx

I think we should probably fix this, but it's probably easier to process this as a googleapi.Error and check the code as suggested here: https://pkg.go.dev/cloud.google.com/go/storage@main#hdr-Errors

If we update this to use the sentinel we'll still keep the wrapping of the googleapi.Error.

tritone avatar Sep 30 '24 16:09 tritone