errors: correct handling 404

This commit is contained in:
Evgeniy Kulikov 2020-04-22 13:34:48 +03:00
parent 96e24653f7
commit 12a3801f3d
No known key found for this signature in database
GPG key ID: BF6AEE0A2A699BF2
2 changed files with 16 additions and 8 deletions

1
go.mod
View file

@ -6,6 +6,7 @@ require (
github.com/fasthttp/router v1.0.2 github.com/fasthttp/router v1.0.2
github.com/nspcc-dev/neofs-api-go v0.7.1 github.com/nspcc-dev/neofs-api-go v0.7.1
github.com/nspcc-dev/neofs-crypto v0.3.0 github.com/nspcc-dev/neofs-crypto v0.3.0
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.5.1 github.com/prometheus/client_golang v1.5.1
github.com/prometheus/common v0.9.1 github.com/prometheus/common v0.9.1
github.com/spf13/pflag v1.0.5 github.com/spf13/pflag v1.0.5

View file

@ -6,16 +6,17 @@ import (
"net/http" "net/http"
"path" "path"
"strconv" "strconv"
"strings"
"time" "time"
"github.com/nspcc-dev/neofs-api-go/container"
"github.com/nspcc-dev/neofs-api-go/object" "github.com/nspcc-dev/neofs-api-go/object"
"github.com/nspcc-dev/neofs-api-go/refs" "github.com/nspcc-dev/neofs-api-go/refs"
"github.com/nspcc-dev/neofs-api-go/service" "github.com/nspcc-dev/neofs-api-go/service"
"github.com/pkg/errors"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
"go.uber.org/zap" "go.uber.org/zap"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
) )
func (a *app) receiveFile(c *fasthttp.RequestCtx) { func (a *app) receiveFile(c *fasthttp.RequestCtx) {
@ -91,14 +92,20 @@ func (a *app) receiveFile(c *fasthttp.RequestCtx) {
zap.Stringer("elapsed", time.Since(start)), zap.Stringer("elapsed", time.Since(start)),
zap.Error(err)) zap.Error(err))
switch { var (
case strings.Contains(err.Error(), object.ErrNotFound.Error()), msg = errors.Wrap(err, "could not receive object").Error()
strings.Contains(err.Error(), container.ErrNotFound.Error()): code = fasthttp.StatusBadRequest
c.Error("object not found", fasthttp.StatusNotFound) )
default:
c.Error("could not receive object", fasthttp.StatusBadRequest) if st, ok := status.FromError(errors.Cause(err)); ok && st != nil {
if st.Code() == codes.NotFound {
code = fasthttp.StatusNotFound
} }
msg = st.Message()
}
c.Error(msg, code)
return return
} }
} }