From 12a3801f3dd5ad17f359a2d02f6fc03312a3c3d9 Mon Sep 17 00:00:00 2001 From: Evgeniy Kulikov Date: Wed, 22 Apr 2020 13:34:48 +0300 Subject: [PATCH] errors: correct handling 404 --- go.mod | 1 + receive.go | 23 +++++++++++++++-------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/go.mod b/go.mod index 80ed0e6..ed473cb 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/fasthttp/router v1.0.2 github.com/nspcc-dev/neofs-api-go v0.7.1 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/common v0.9.1 github.com/spf13/pflag v1.0.5 diff --git a/receive.go b/receive.go index de2d148..7f09ff4 100644 --- a/receive.go +++ b/receive.go @@ -6,16 +6,17 @@ import ( "net/http" "path" "strconv" - "strings" "time" - "github.com/nspcc-dev/neofs-api-go/container" "github.com/nspcc-dev/neofs-api-go/object" "github.com/nspcc-dev/neofs-api-go/refs" "github.com/nspcc-dev/neofs-api-go/service" + "github.com/pkg/errors" "github.com/valyala/fasthttp" "go.uber.org/zap" "google.golang.org/grpc" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" ) 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.Error(err)) - switch { - case strings.Contains(err.Error(), object.ErrNotFound.Error()), - strings.Contains(err.Error(), container.ErrNotFound.Error()): - c.Error("object not found", fasthttp.StatusNotFound) - default: - c.Error("could not receive object", fasthttp.StatusBadRequest) + var ( + msg = errors.Wrap(err, "could not receive object").Error() + code = 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 } }