From 63652d213cdaec512f753aa4887084cd7dd51355 Mon Sep 17 00:00:00 2001 From: Denis Kirillov Date: Mon, 12 Jul 2021 15:04:27 +0300 Subject: [PATCH] [#149] Removed grpc errors Signed-off-by: Denis Kirillov --- api/handler/delete.go | 6 ------ api/layer/layer.go | 12 +----------- api/layer/object.go | 18 +++++++++++------- 3 files changed, 12 insertions(+), 24 deletions(-) diff --git a/api/handler/delete.go b/api/handler/delete.go index f6cc9945..1898949c 100644 --- a/api/handler/delete.go +++ b/api/handler/delete.go @@ -8,7 +8,6 @@ import ( "github.com/nspcc-dev/neofs-s3-gw/api" "github.com/nspcc-dev/neofs-s3-gw/api/layer" "go.uber.org/zap" - "google.golang.org/grpc/status" ) // DeleteObjectsRequest - xml carrying the object key names which needs to be deleted. @@ -122,11 +121,6 @@ func (h *handler) DeleteMultipleObjectsHandler(w http.ResponseWriter, r *http.Re code := "BadRequest" desc := err.Error() - if st, ok := status.FromError(err.Err); ok && st != nil { - desc = st.Message() - code = st.Code().String() - } - response.Errors = append(response.Errors, DeleteError{ Code: code, Message: desc, diff --git a/api/layer/layer.go b/api/layer/layer.go index a3d348b9..4718b162 100644 --- a/api/layer/layer.go +++ b/api/layer/layer.go @@ -19,8 +19,6 @@ import ( "github.com/nspcc-dev/neofs-s3-gw/creds/accessbox" "github.com/nspcc-dev/neofs-sdk-go/pkg/pool" "go.uber.org/zap" - "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" ) type ( @@ -123,10 +121,6 @@ type ( ) var ( - // ErrObjectExists is returned on attempts to create already existing object. - ErrObjectExists = errors.New("object exists") - // ErrObjectNotExists is returned on attempts to work with non-existing object. - ErrObjectNotExists = errors.New("object not exists") // ErrBucketAlreadyExists is returned on attempts to create already existing bucket. ErrBucketAlreadyExists = errors.New("bucket exists") // ErrBucketNotFound is returned on attempts to get not existing bucket. @@ -257,11 +251,7 @@ func (n *layer) checkObject(ctx context.Context, cid *cid.ID, filename string) e var err error if _, err = n.objectFindID(ctx, &findParams{cid: cid, val: filename}); err == nil { - return ErrObjectExists - } else if state, ok := status.FromError(err); !ok || state == nil { - return err - } else if state.Code() == codes.NotFound { - return ErrObjectNotExists + return new(api.ObjectAlreadyExists) } return err diff --git a/api/layer/object.go b/api/layer/object.go index 3116aad5..fb31e220 100644 --- a/api/layer/object.go +++ b/api/layer/object.go @@ -81,7 +81,7 @@ func (n *layer) objectFindID(ctx context.Context, p *findParams) (*object.ID, er if result, err := n.objectSearch(ctx, p); err != nil { return nil, err } else if ln := len(result); ln == 0 { - return nil, &api.ObjectNotFound{Bucket: p.cid.String(), Object: p.val} + return nil, api.GetAPIError(api.ErrNoSuchKey) } else if ln == 1 { return result[0], nil } @@ -123,12 +123,16 @@ func (n *layer) objectPut(ctx context.Context, p *PutObjectParams) (*ObjectInfo, return nil, err } else if bkt, err = n.GetBucketInfo(ctx, p.Bucket); err != nil { return nil, err - } else if err = n.checkObject(ctx, bkt.CID, p.Object); err != nil && err != ErrObjectNotExists { - return nil, err - } else if err == ErrObjectExists { - return nil, &api.ObjectAlreadyExists{ - Bucket: p.Bucket, - Object: p.Object, + } else if err = n.checkObject(ctx, bkt.CID, p.Object); err != nil { + var errExist *api.ObjectAlreadyExists + if ok := errors.As(err, &errExist); ok { + errExist.Bucket = p.Bucket + errExist.Object = p.Object + return nil, errExist + } + + if !api.IsS3Error(err, api.ErrNoSuchKey) { + return nil, err } }