[#125] Updated error handling

Signed-off-by: Denis Kirillov <denis@nspcc.ru>
This commit is contained in:
Denis Kirillov 2021-07-09 11:57:44 +03:00
parent e11b1b76ba
commit e78543adf3
4 changed files with 13 additions and 13 deletions

View file

@ -3,14 +3,13 @@ package handler
import ( import (
"bytes" "bytes"
"context" "context"
"errors"
"net/http" "net/http"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/nspcc-dev/neofs-s3-gw/api" "github.com/nspcc-dev/neofs-s3-gw/api"
"github.com/nspcc-dev/neofs-s3-gw/api/layer" "github.com/nspcc-dev/neofs-s3-gw/api/layer"
"go.uber.org/zap" "go.uber.org/zap"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
) )
const sizeToDetectType = 512 const sizeToDetectType = 512
@ -125,13 +124,8 @@ func (h *handler) HeadBucketHandler(w http.ResponseWriter, r *http.Request) {
zap.Error(err)) zap.Error(err))
code := http.StatusBadRequest code := http.StatusBadRequest
if st, ok := status.FromError(err); ok && st != nil { if errors.Is(err, layer.ErrBucketNotFound) {
switch st.Code() { //nolint:exhaustive // we have default value set above code = http.StatusNotFound
case codes.NotFound:
code = http.StatusNotFound
case codes.PermissionDenied:
code = http.StatusForbidden
}
} }
api.WriteResponse(w, code, nil, api.MimeNone) api.WriteResponse(w, code, nil, api.MimeNone)

View file

@ -100,6 +100,7 @@ func (h *handler) CreateBucketHandler(w http.ResponseWriter, r *http.Request) {
Description: err.Error(), Description: err.Error(),
HTTPStatusCode: http.StatusBadRequest, HTTPStatusCode: http.StatusBadRequest,
}, r.URL) }, r.URL)
return
} }
p.Policy, err = policy.Parse(defaultPolicy) p.Policy, err = policy.Parse(defaultPolicy)
@ -113,6 +114,7 @@ func (h *handler) CreateBucketHandler(w http.ResponseWriter, r *http.Request) {
Description: err.Error(), Description: err.Error(),
HTTPStatusCode: http.StatusBadRequest, HTTPStatusCode: http.StatusBadRequest,
}, r.URL) }, r.URL)
return
} }
cid, err := h.obj.CreateBucket(r.Context(), &p) cid, err := h.obj.CreateBucket(r.Context(), &p)

View file

@ -5,6 +5,7 @@ import (
"crypto/ecdsa" "crypto/ecdsa"
"fmt" "fmt"
"strconv" "strconv"
"strings"
"time" "time"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys" "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.String("request_id", rid),
zap.Error(err)) zap.Error(err))
if strings.Contains(err.Error(), "container not found") {
return nil, ErrBucketNotFound
}
return nil, err return nil, err
} }

View file

@ -129,6 +129,8 @@ var (
ErrObjectNotExists = errors.New("object not exists") ErrObjectNotExists = errors.New("object not exists")
// ErrBucketAlreadyExists is returned on attempts to create already existing bucket. // ErrBucketAlreadyExists is returned on attempts to create already existing bucket.
ErrBucketAlreadyExists = errors.New("bucket exists") ErrBucketAlreadyExists = errors.New("bucket exists")
// ErrBucketNotFound is returned on attempts to get not existing bucket.
ErrBucketNotFound = errors.New("bucket not found")
) )
const ( 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) 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) { func (n *layer) CreateBucket(ctx context.Context, p *CreateBucketParams) (*cid.ID, error) {
_, err := n.GetBucketInfo(ctx, p.Name) _, err := n.GetBucketInfo(ctx, p.Name)
if err != nil { if err != nil {
errMsg := err.Error() if errors.Is(err, ErrBucketNotFound) {
if strings.Contains(errMsg, "bucket not found") ||
strings.Contains(errMsg, "container not found") {
return n.createContainer(ctx, p) return n.createContainer(ctx, p)
} }
return nil, err return nil, err