From e78543adf3f34d69f77fbd13c967ce1afc6248d4 Mon Sep 17 00:00:00 2001 From: Denis Kirillov Date: Fri, 9 Jul 2021 11:57:44 +0300 Subject: [PATCH] [#125] Updated error handling Signed-off-by: Denis Kirillov --- api/handler/head.go | 12 +++--------- api/handler/put.go | 2 ++ api/layer/container.go | 4 ++++ api/layer/layer.go | 8 ++++---- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/api/handler/head.go b/api/handler/head.go index e3b75806..c916b7d6 100644 --- a/api/handler/head.go +++ b/api/handler/head.go @@ -3,14 +3,13 @@ package handler import ( "bytes" "context" + "errors" "net/http" "github.com/gorilla/mux" "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/codes" - "google.golang.org/grpc/status" ) const sizeToDetectType = 512 @@ -125,13 +124,8 @@ func (h *handler) HeadBucketHandler(w http.ResponseWriter, r *http.Request) { zap.Error(err)) code := http.StatusBadRequest - if st, ok := status.FromError(err); ok && st != nil { - switch st.Code() { //nolint:exhaustive // we have default value set above - case codes.NotFound: - code = http.StatusNotFound - case codes.PermissionDenied: - code = http.StatusForbidden - } + if errors.Is(err, layer.ErrBucketNotFound) { + code = http.StatusNotFound } api.WriteResponse(w, code, nil, api.MimeNone) diff --git a/api/handler/put.go b/api/handler/put.go index 73b5d0b9..40ff6432 100644 --- a/api/handler/put.go +++ b/api/handler/put.go @@ -100,6 +100,7 @@ func (h *handler) CreateBucketHandler(w http.ResponseWriter, r *http.Request) { Description: err.Error(), HTTPStatusCode: http.StatusBadRequest, }, r.URL) + return } p.Policy, err = policy.Parse(defaultPolicy) @@ -113,6 +114,7 @@ func (h *handler) CreateBucketHandler(w http.ResponseWriter, r *http.Request) { Description: err.Error(), HTTPStatusCode: http.StatusBadRequest, }, r.URL) + return } cid, err := h.obj.CreateBucket(r.Context(), &p) diff --git a/api/layer/container.go b/api/layer/container.go index 0e10a2fd..36ef2de4 100644 --- a/api/layer/container.go +++ b/api/layer/container.go @@ -5,6 +5,7 @@ import ( "crypto/ecdsa" "fmt" "strconv" + "strings" "time" "github.com/nspcc-dev/neo-go/pkg/crypto/keys" @@ -58,6 +59,9 @@ func (n *layer) containerInfo(ctx context.Context, cid *cid.ID) (*BucketInfo, er zap.String("request_id", rid), zap.Error(err)) + if strings.Contains(err.Error(), "container not found") { + return nil, ErrBucketNotFound + } return nil, err } diff --git a/api/layer/layer.go b/api/layer/layer.go index e40380f5..328dbea4 100644 --- a/api/layer/layer.go +++ b/api/layer/layer.go @@ -129,6 +129,8 @@ var ( 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. + ErrBucketNotFound = errors.New("bucket not found") ) const ( @@ -198,7 +200,7 @@ func (n *layer) GetBucketInfo(ctx context.Context, name string) (*BucketInfo, er } } - return nil, status.Error(codes.NotFound, "bucket not found") + return nil, ErrBucketNotFound } return n.containerInfo(ctx, containerID) @@ -495,9 +497,7 @@ func (n *layer) DeleteObjects(ctx context.Context, bucket string, objects []stri func (n *layer) CreateBucket(ctx context.Context, p *CreateBucketParams) (*cid.ID, error) { _, err := n.GetBucketInfo(ctx, p.Name) if err != nil { - errMsg := err.Error() - if strings.Contains(errMsg, "bucket not found") || - strings.Contains(errMsg, "container not found") { + if errors.Is(err, ErrBucketNotFound) { return n.createContainer(ctx, p) } return nil, err