Merge pull request #171 from KirillovDenis/feature/149-remove_grpc_errors

[#149] Remove grpc errors
This commit is contained in:
Alex Vanin 2021-07-26 13:25:28 +03:00 committed by GitHub
commit bcf9758a7e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 307 additions and 396 deletions

File diff suppressed because it is too large Load diff

26
api/errors_test.go Normal file
View file

@ -0,0 +1,26 @@
package api
import (
"errors"
"testing"
)
func BenchmarkErrCode(b *testing.B) {
err := GetAPIError(ErrNoSuchKey)
for i := 0; i < b.N; i++ {
if IsS3Error(err, ErrNoSuchKey) {
_ = err
}
}
}
func BenchmarkErrorsIs(b *testing.B) {
err := GetAPIError(ErrNoSuchKey)
for i := 0; i < b.N; i++ {
if errors.Is(err, GetAPIError(ErrNoSuchKey)) {
_ = err
}
}
}

View file

@ -8,7 +8,6 @@ import (
"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/status"
) )
// DeleteObjectsRequest - xml carrying the object key names which needs to be deleted. // 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" code := "BadRequest"
desc := err.Error() 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{ response.Errors = append(response.Errors, DeleteError{
Code: code, Code: code,
Message: desc, Message: desc,

View file

@ -2,7 +2,6 @@ package handler
import ( import (
"bytes" "bytes"
"errors"
"net/http" "net/http"
"github.com/gorilla/mux" "github.com/gorilla/mux"
@ -43,10 +42,6 @@ func (h *handler) HeadObjectHandler(w http.ResponseWriter, r *http.Request) {
zap.String("object_name", obj), zap.String("object_name", obj),
zap.Error(err)) zap.Error(err))
var genErr *api.ObjectNotFound
if ok := errors.As(err, &genErr); ok {
err = api.GetAPIError(api.ErrNoSuchKey)
}
api.WriteErrorResponse(r.Context(), w, err, r.URL) api.WriteErrorResponse(r.Context(), w, err, r.URL)
return return
} }
@ -87,13 +82,7 @@ func (h *handler) HeadBucketHandler(w http.ResponseWriter, r *http.Request) {
zap.String("bucket_name", bkt), zap.String("bucket_name", bkt),
zap.Error(err)) zap.Error(err))
code := http.StatusBadRequest api.WriteErrorResponse(r.Context(), w, err, r.URL)
if errors.Is(err, layer.ErrBucketNotFound) {
code = http.StatusNotFound
}
api.WriteResponse(w, code, nil, api.MimeNone)
return return
} }

View file

@ -50,7 +50,7 @@ func (n *layer) containerInfo(ctx context.Context, cid *cid.ID) (*BucketInfo, er
zap.Error(err)) zap.Error(err))
if strings.Contains(err.Error(), "container not found") { if strings.Contains(err.Error(), "container not found") {
return nil, ErrBucketNotFound return nil, api.GetAPIError(api.ErrNoSuchBucket)
} }
return nil, err return nil, err
} }

View file

@ -3,7 +3,6 @@ package layer
import ( import (
"context" "context"
"crypto/ecdsa" "crypto/ecdsa"
"errors"
"fmt" "fmt"
"io" "io"
"net/url" "net/url"
@ -19,8 +18,6 @@ import (
"github.com/nspcc-dev/neofs-s3-gw/creds/accessbox" "github.com/nspcc-dev/neofs-s3-gw/creds/accessbox"
"github.com/nspcc-dev/neofs-sdk-go/pkg/pool" "github.com/nspcc-dev/neofs-sdk-go/pkg/pool"
"go.uber.org/zap" "go.uber.org/zap"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
) )
type ( type (
@ -122,17 +119,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.
ErrBucketNotFound = errors.New("bucket not found")
)
const ( const (
unversionedObjectVersionID = "null" unversionedObjectVersionID = "null"
) )
@ -198,7 +184,7 @@ func (n *layer) GetBucketInfo(ctx context.Context, name string) (*BucketInfo, er
} }
} }
return nil, ErrBucketNotFound return nil, api.GetAPIError(api.ErrNoSuchBucket)
} }
return n.containerInfo(ctx, containerID) return n.containerInfo(ctx, containerID)
@ -257,11 +243,7 @@ func (n *layer) checkObject(ctx context.Context, cid *cid.ID, filename string) e
var err error var err error
if _, err = n.objectFindID(ctx, &findParams{cid: cid, val: filename}); err == nil { if _, err = n.objectFindID(ctx, &findParams{cid: cid, val: filename}); err == nil {
return ErrObjectExists return new(api.ObjectAlreadyExists)
} else if state, ok := status.FromError(err); !ok || state == nil {
return err
} else if state.Code() == codes.NotFound {
return ErrObjectNotExists
} }
return err return err
@ -378,13 +360,13 @@ 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 {
if errors.Is(err, ErrBucketNotFound) { if api.IsS3Error(err, api.ErrNoSuchBucket) {
return n.createContainer(ctx, p) return n.createContainer(ctx, p)
} }
return nil, err return nil, err
} }
return nil, ErrBucketAlreadyExists return nil, api.GetAPIError(api.ErrBucketAlreadyExists)
} }
func (n *layer) DeleteBucket(ctx context.Context, p *DeleteBucketParams) error { func (n *layer) DeleteBucket(ctx context.Context, p *DeleteBucketParams) error {

View file

@ -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 { if result, err := n.objectSearch(ctx, p); err != nil {
return nil, err return nil, err
} else if ln := len(result); ln == 0 { } 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 { } else if ln == 1 {
return result[0], nil return result[0], nil
} }
@ -123,12 +123,16 @@ func (n *layer) objectPut(ctx context.Context, p *PutObjectParams) (*ObjectInfo,
return nil, err return nil, err
} else if bkt, err = n.GetBucketInfo(ctx, p.Bucket); err != nil { } else if bkt, err = n.GetBucketInfo(ctx, p.Bucket); err != nil {
return nil, err return nil, err
} else if err = n.checkObject(ctx, bkt.CID, p.Object); err != nil && err != ErrObjectNotExists { } 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 return nil, err
} else if err == ErrObjectExists {
return nil, &api.ObjectAlreadyExists{
Bucket: p.Bucket,
Object: p.Object,
} }
} }

View file

@ -38,6 +38,7 @@ type (
// Error structure represents API error. // Error structure represents API error.
Error struct { Error struct {
ErrCode ErrorCode
Code string Code string
Description string Description string
HTTPStatusCode int HTTPStatusCode int