From b567a08a851f3accc311d0caf50a072ef16deb9f Mon Sep 17 00:00:00 2001 From: Denis Kirillov Date: Mon, 26 Sep 2022 16:02:04 +0300 Subject: [PATCH] [#206] Refactor neofs 'not found' errors Signed-off-by: Denis Kirillov --- downloader/download.go | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/downloader/download.go b/downloader/download.go index bb48b51..6891ef7 100644 --- a/downloader/download.go +++ b/downloader/download.go @@ -22,6 +22,7 @@ import ( "github.com/nspcc-dev/neofs-http-gw/tokens" "github.com/nspcc-dev/neofs-http-gw/utils" "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" "github.com/nspcc-dev/neofs-sdk-go/object" oid "github.com/nspcc-dev/neofs-sdk-go/object/id" @@ -37,8 +38,6 @@ type request struct { log *zap.Logger } -var errObjectNotFound = errors.New("object not found") - const attributeFilePath = "FilePath" 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.Error(err), ) - var ( - msg = fmt.Sprintf("could not receive object: %v", err) - code = fasthttp.StatusBadRequest - cause = err - ) - for unwrap := errors.Unwrap(err); unwrap != nil; unwrap = errors.Unwrap(cause) { - cause = unwrap + + if client.IsErrObjectNotFound(err) || client.IsErrContainerNotFound(err) { + response.Error(r.RequestCtx, "Not Found", fasthttp.StatusNotFound) + return } - if strings.Contains(cause.Error(), "not found") || - strings.Contains(cause.Error(), "can't fetch container info") { - code = fasthttp.StatusNotFound - msg = errObjectNotFound.Error() - } - - response.Error(r.RequestCtx, msg, code) + msg := fmt.Sprintf("could not receive object: %v", err) + response.Error(r.RequestCtx, msg, fasthttp.StatusBadRequest) } // Downloader is a download request handler.