[#149] Refactoring

Signed-off-by: Denis Kirillov <denis@nspcc.ru>
This commit is contained in:
Denis Kirillov 2021-07-12 16:18:52 +03:00
parent 63652d213c
commit 28974474f2
5 changed files with 268 additions and 34 deletions

File diff suppressed because it is too large Load diff

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"
@ -120,13 +119,6 @@ type (
} }
) )
var (
// 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"
) )
@ -192,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)
@ -368,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

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