Skip to content

Commit

Permalink
fix(datastore): handle not found errors from azure, s3 and gcs (#511)
Browse files Browse the repository at this point in the history
  • Loading branch information
corrieriluca authored Jan 17, 2025
1 parent cb02ead commit af6af4c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 5 deletions.
6 changes: 6 additions & 0 deletions internal/datastore/storage/azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
39 changes: 35 additions & 4 deletions internal/datastore/storage/gcs/gcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
26 changes: 25 additions & 1 deletion internal/datastore/storage/s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit af6af4c

Please sign in to comment.