From af6af4c95c692445b407f2f83c57d80f7e68393d Mon Sep 17 00:00:00 2001 From: Luca Corrieri Date: Fri, 17 Jan 2025 13:17:02 +0100 Subject: [PATCH] fix(datastore): handle not found errors from azure, s3 and gcs (#511) --- internal/datastore/storage/azure/azure.go | 6 ++++ internal/datastore/storage/gcs/gcs.go | 39 ++++++++++++++++++++--- internal/datastore/storage/s3/s3.go | 26 ++++++++++++++- 3 files changed, 66 insertions(+), 5 deletions(-) diff --git a/internal/datastore/storage/azure/azure.go b/internal/datastore/storage/azure/azure.go index 7b7c8b9b..78a48dfb 100644 --- a/internal/datastore/storage/azure/azure.go +++ b/internal/datastore/storage/azure/azure.go @@ -84,6 +84,12 @@ func (a *Azure) Set(key string, value []byte, ttl int) error { func (a *Azure) Delete(key string) error { _, err := a.Client.DeleteBlob(context.Background(), a.Config.Container, key, nil) + if bloberror.HasCode(err, bloberror.BlobNotFound) { + return &errors.StorageError{ + Err: err, + Nil: true, + } + } if err != nil { return &errors.StorageError{ Err: err, diff --git a/internal/datastore/storage/gcs/gcs.go b/internal/datastore/storage/gcs/gcs.go index b27c6ea2..5883c651 100644 --- a/internal/datastore/storage/gcs/gcs.go +++ b/internal/datastore/storage/gcs/gcs.go @@ -6,6 +6,7 @@ import ( "cloud.google.com/go/storage" "github.com/padok-team/burrito/internal/burrito/config" + errors "github.com/padok-team/burrito/internal/datastore/storage/error" "google.golang.org/api/iterator" ) @@ -33,14 +34,26 @@ func (a *GCS) Get(key string) ([]byte, error) { bucket := a.Client.Bucket(a.Config.Bucket) obj := bucket.Object(key) reader, err := obj.NewReader(ctx) + if err == storage.ErrObjectNotExist { + return make([]byte, 0), &errors.StorageError{ + Err: err, + Nil: true, + } + } if err != nil { - return nil, err + return make([]byte, 0), &errors.StorageError{ + Err: err, + Nil: false, + } } defer reader.Close() data, err := io.ReadAll(reader) if err != nil { - return nil, err + return make([]byte, 0), &errors.StorageError{ + Err: err, + Nil: false, + } } return data, nil @@ -55,7 +68,10 @@ func (a *GCS) Set(key string, data []byte, ttl int) error { _, err := writer.Write(data) if err != nil { - return err + return &errors.StorageError{ + Err: err, + Nil: false, + } } return nil @@ -66,8 +82,17 @@ func (a *GCS) Check(key string) ([]byte, error) { bucket := a.Client.Bucket(a.Config.Bucket) obj := bucket.Object(key) metadata, err := obj.Attrs(ctx) + if err == storage.ErrObjectNotExist { + return make([]byte, 0), &errors.StorageError{ + Err: err, + Nil: true, + } + } if err != nil { - return nil, err + return make([]byte, 0), &errors.StorageError{ + Err: err, + Nil: false, + } } return metadata.MD5, nil } @@ -77,6 +102,12 @@ func (a *GCS) Delete(key string) error { bucket := a.Client.Bucket(a.Config.Bucket) obj := bucket.Object(key) err := obj.Delete(ctx) + if err == storage.ErrObjectNotExist { + return &errors.StorageError{ + Err: err, + Nil: true, + } + } if err != nil { return err } diff --git a/internal/datastore/storage/s3/s3.go b/internal/datastore/storage/s3/s3.go index 18cd39a3..6b7de0d2 100644 --- a/internal/datastore/storage/s3/s3.go +++ b/internal/datastore/storage/s3/s3.go @@ -3,13 +3,16 @@ package s3 import ( "bytes" "context" + "errors" "fmt" "io" sdk "github.com/aws/aws-sdk-go-v2/config" storage "github.com/aws/aws-sdk-go-v2/service/s3" + "github.com/aws/aws-sdk-go-v2/service/s3/types" "github.com/aws/aws-sdk-go/aws" "github.com/padok-team/burrito/internal/burrito/config" + storageerrors "github.com/padok-team/burrito/internal/datastore/storage/error" ) // Implements Storage interface using AWS S3 @@ -41,6 +44,13 @@ func (a *S3) Get(key string) ([]byte, error) { result, err := a.Client.GetObject(context.TODO(), input) if err != nil { + var noKey *types.NoSuchKey + if errors.As(err, &noKey) { + return nil, &storageerrors.StorageError{ + Err: err, + Nil: true, + } + } return nil, err } @@ -62,7 +72,14 @@ func (a *S3) Check(key string) ([]byte, error) { result, err := a.Client.HeadObject(context.TODO(), input) if err != nil { - return nil, err + var noKey *types.NoSuchKey + if errors.As(err, &noKey) { + return make([]byte, 0), &storageerrors.StorageError{ + Err: err, + Nil: true, + } + } + return make([]byte, 0), err } return []byte(*result.ChecksumSHA256), nil @@ -91,6 +108,13 @@ func (a *S3) Delete(key string) error { _, err := a.Client.DeleteObject(context.TODO(), input) if err != nil { + var noKey *types.NoSuchKey + if errors.As(err, &noKey) { + return &storageerrors.StorageError{ + Err: err, + Nil: true, + } + } return err }