[#206] Refactor neofs 'not found' errors

Signed-off-by: Denis Kirillov <denis@nspcc.ru>
This commit is contained in:
Denis Kirillov 2022-09-26 16:02:04 +03:00 committed by Alex Vanin
parent 3fa0e3caef
commit b567a08a85

View file

@ -22,6 +22,7 @@ import (
"github.com/nspcc-dev/neofs-http-gw/tokens" "github.com/nspcc-dev/neofs-http-gw/tokens"
"github.com/nspcc-dev/neofs-http-gw/utils" "github.com/nspcc-dev/neofs-http-gw/utils"
"github.com/nspcc-dev/neofs-sdk-go/bearer" "github.com/nspcc-dev/neofs-sdk-go/bearer"
"github.com/nspcc-dev/neofs-sdk-go/client"
cid "github.com/nspcc-dev/neofs-sdk-go/container/id" cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
"github.com/nspcc-dev/neofs-sdk-go/object" "github.com/nspcc-dev/neofs-sdk-go/object"
oid "github.com/nspcc-dev/neofs-sdk-go/object/id" oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
@ -37,8 +38,6 @@ type request struct {
log *zap.Logger log *zap.Logger
} }
var errObjectNotFound = errors.New("object not found")
const attributeFilePath = "FilePath" const attributeFilePath = "FilePath"
func isValidToken(s string) bool { func isValidToken(s string) bool {
@ -232,22 +231,14 @@ func (r *request) handleNeoFSErr(err error, start time.Time) {
zap.Stringer("elapsed", time.Since(start)), zap.Stringer("elapsed", time.Since(start)),
zap.Error(err), zap.Error(err),
) )
var (
msg = fmt.Sprintf("could not receive object: %v", err) if client.IsErrObjectNotFound(err) || client.IsErrContainerNotFound(err) {
code = fasthttp.StatusBadRequest response.Error(r.RequestCtx, "Not Found", fasthttp.StatusNotFound)
cause = err return
)
for unwrap := errors.Unwrap(err); unwrap != nil; unwrap = errors.Unwrap(cause) {
cause = unwrap
} }
if strings.Contains(cause.Error(), "not found") || msg := fmt.Sprintf("could not receive object: %v", err)
strings.Contains(cause.Error(), "can't fetch container info") { response.Error(r.RequestCtx, msg, fasthttp.StatusBadRequest)
code = fasthttp.StatusNotFound
msg = errObjectNotFound.Error()
}
response.Error(r.RequestCtx, msg, code)
} }
// Downloader is a download request handler. // Downloader is a download request handler.