[#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 (
"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:
if errors.Is(err, layer.ErrBucketNotFound) {
code = http.StatusNotFound
case codes.PermissionDenied:
code = http.StatusForbidden
}
}
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(),
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)

View file

@ -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
}

View file

@ -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