[#105] Add newline to errors

Signed-off-by: Denis Kirillov <denis@nspcc.ru>
This commit is contained in:
Denis Kirillov 2021-11-12 14:37:05 +03:00 committed by Alex Vanin
parent 3ef46151d2
commit 6265fcb26a
7 changed files with 40 additions and 21 deletions

7
app.go
View file

@ -13,6 +13,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/wallet" "github.com/nspcc-dev/neo-go/pkg/wallet"
"github.com/nspcc-dev/neofs-http-gw/downloader" "github.com/nspcc-dev/neofs-http-gw/downloader"
"github.com/nspcc-dev/neofs-http-gw/response"
"github.com/nspcc-dev/neofs-http-gw/uploader" "github.com/nspcc-dev/neofs-http-gw/uploader"
"github.com/nspcc-dev/neofs-sdk-go/pkg/logger" "github.com/nspcc-dev/neofs-sdk-go/pkg/logger"
"github.com/nspcc-dev/neofs-sdk-go/pkg/pool" "github.com/nspcc-dev/neofs-sdk-go/pkg/pool"
@ -197,6 +198,12 @@ func (a *app) Serve(ctx context.Context) {
// Configure router. // Configure router.
r := router.New() r := router.New()
r.RedirectTrailingSlash = true r.RedirectTrailingSlash = true
r.NotFound = func(r *fasthttp.RequestCtx) {
response.Error(r, "Not found", fasthttp.StatusNotFound)
}
r.MethodNotAllowed = func(r *fasthttp.RequestCtx) {
response.Error(r, "Method Not Allowed", fasthttp.StatusMethodNotAllowed)
}
r.POST("/upload/{cid}", a.logger(uploader.Upload)) r.POST("/upload/{cid}", a.logger(uploader.Upload))
a.log.Info("added path /upload/{cid}") a.log.Info("added path /upload/{cid}")
r.GET("/get/{cid}/{oid}", a.logger(downloader.DownloadByAddress)) r.GET("/get/{cid}/{oid}", a.logger(downloader.DownloadByAddress))

View file

@ -15,6 +15,7 @@ import (
"github.com/nspcc-dev/neofs-api-go/pkg/client" "github.com/nspcc-dev/neofs-api-go/pkg/client"
cid "github.com/nspcc-dev/neofs-api-go/pkg/container/id" cid "github.com/nspcc-dev/neofs-api-go/pkg/container/id"
"github.com/nspcc-dev/neofs-api-go/pkg/object" "github.com/nspcc-dev/neofs-api-go/pkg/object"
"github.com/nspcc-dev/neofs-http-gw/response"
"github.com/nspcc-dev/neofs-http-gw/tokens" "github.com/nspcc-dev/neofs-http-gw/tokens"
"github.com/nspcc-dev/neofs-sdk-go/pkg/pool" "github.com/nspcc-dev/neofs-sdk-go/pkg/pool"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
@ -123,7 +124,7 @@ func (r request) receiveFile(clnt client.Object, objectAddress *object.Address)
) )
if err = tokens.StoreBearerToken(r.RequestCtx); err != nil { if err = tokens.StoreBearerToken(r.RequestCtx); err != nil {
r.log.Error("could not fetch and store bearer token", zap.Error(err)) r.log.Error("could not fetch and store bearer token", zap.Error(err))
r.Error("could not fetch and store bearer token", fasthttp.StatusBadRequest) response.Error(r.RequestCtx, "could not fetch and store bearer token", fasthttp.StatusBadRequest)
return return
} }
readDetector := newDetector() readDetector := newDetector()
@ -177,7 +178,7 @@ func (r request) receiveFile(clnt client.Object, objectAddress *object.Address)
if len(contentType) == 0 { if len(contentType) == 0 {
if readDetector.err != nil { if readDetector.err != nil {
r.log.Error("could not read object", zap.Error(err)) r.log.Error("could not read object", zap.Error(err))
r.Error("could not read object", fasthttp.StatusBadRequest) response.Error(r.RequestCtx, "could not read object", fasthttp.StatusBadRequest)
return return
} }
readDetector.Wait() readDetector.Wait()
@ -216,7 +217,7 @@ func (r *request) handleNeoFSErr(err error, start time.Time) {
msg = errObjectNotFound.Error() msg = errObjectNotFound.Error()
} }
r.Error(msg, code) response.Error(r.RequestCtx, msg, code)
} }
func (o objectIDs) Slice() []string { func (o objectIDs) Slice() []string {
@ -272,7 +273,7 @@ func (d *Downloader) byAddress(c *fasthttp.RequestCtx, f func(request, client.Ob
) )
if err := address.Parse(val); err != nil { if err := address.Parse(val); err != nil {
log.Error("wrong object address", zap.Error(err)) log.Error("wrong object address", zap.Error(err))
c.Error("wrong object address", fasthttp.StatusBadRequest) response.Error(c, "wrong object address", fasthttp.StatusBadRequest)
return return
} }
@ -296,7 +297,7 @@ func (d *Downloader) byAttribute(c *fasthttp.RequestCtx, f func(request, client.
containerID := cid.New() containerID := cid.New()
if err := containerID.Parse(scid); err != nil { if err := containerID.Parse(scid); err != nil {
log.Error("wrong container id", zap.Error(err)) log.Error("wrong container id", zap.Error(err))
c.Error("wrong container id", httpStatus) response.Error(c, "wrong container id", httpStatus)
return return
} }
@ -306,7 +307,7 @@ func (d *Downloader) byAttribute(c *fasthttp.RequestCtx, f func(request, client.
if errors.Is(err, errObjectNotFound) { if errors.Is(err, errObjectNotFound) {
httpStatus = fasthttp.StatusNotFound httpStatus = fasthttp.StatusNotFound
} }
c.Error("couldn't search object", httpStatus) response.Error(c, "couldn't search object", httpStatus)
return return
} }
@ -368,13 +369,13 @@ func (d *Downloader) DownloadZipped(c *fasthttp.RequestCtx) {
containerID := cid.New() containerID := cid.New()
if err := containerID.Parse(scid); err != nil { if err := containerID.Parse(scid); err != nil {
log.Error("wrong container id", zap.Error(err)) log.Error("wrong container id", zap.Error(err))
c.Error("wrong container id", status) response.Error(c, "wrong container id", status)
return return
} }
if err := tokens.StoreBearerToken(c); err != nil { if err := tokens.StoreBearerToken(c); err != nil {
log.Error("could not fetch and store bearer token", zap.Error(err)) log.Error("could not fetch and store bearer token", zap.Error(err))
c.Error("could not fetch and store bearer token", fasthttp.StatusBadRequest) response.Error(c, "could not fetch and store bearer token", fasthttp.StatusBadRequest)
return return
} }
@ -384,7 +385,7 @@ func (d *Downloader) DownloadZipped(c *fasthttp.RequestCtx) {
if errors.Is(err, errObjectNotFound) { if errors.Is(err, errObjectNotFound) {
status = fasthttp.StatusNotFound status = fasthttp.StatusNotFound
} }
c.Error("couldn't find objects", status) response.Error(c, "couldn't find objects", status)
return return
} }
@ -394,7 +395,7 @@ func (d *Downloader) DownloadZipped(c *fasthttp.RequestCtx) {
if err = d.streamFiles(c, containerID, ids); err != nil { if err = d.streamFiles(c, containerID, ids); err != nil {
log.Error("couldn't stream files", zap.Error(err)) log.Error("couldn't stream files", zap.Error(err))
c.Error("couldn't stream", fasthttp.StatusInternalServerError) response.Error(c, "couldn't stream", fasthttp.StatusInternalServerError)
return return
} }
} }

View file

@ -7,6 +7,7 @@ import (
"github.com/nspcc-dev/neofs-api-go/pkg/client" "github.com/nspcc-dev/neofs-api-go/pkg/client"
"github.com/nspcc-dev/neofs-api-go/pkg/object" "github.com/nspcc-dev/neofs-api-go/pkg/object"
"github.com/nspcc-dev/neofs-http-gw/response"
"github.com/nspcc-dev/neofs-http-gw/tokens" "github.com/nspcc-dev/neofs-http-gw/tokens"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
"go.uber.org/zap" "go.uber.org/zap"
@ -18,7 +19,7 @@ func (r request) headObject(clnt client.Object, objectAddress *object.Address) {
var start = time.Now() var start = time.Now()
if err := tokens.StoreBearerToken(r.RequestCtx); err != nil { if err := tokens.StoreBearerToken(r.RequestCtx); err != nil {
r.log.Error("could not fetch and store bearer token", zap.Error(err)) r.log.Error("could not fetch and store bearer token", zap.Error(err))
r.Error("could not fetch and store bearer token", fasthttp.StatusBadRequest) response.Error(r.RequestCtx, "could not fetch and store bearer token", fasthttp.StatusBadRequest)
return return
} }

View file

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"github.com/fasthttp/router" "github.com/fasthttp/router"
"github.com/nspcc-dev/neofs-http-gw/response"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/expfmt" "github.com/prometheus/common/expfmt"
@ -57,7 +58,7 @@ func metricsHandler(reg prometheus.Gatherer, opts promhttp.HandlerOpts) fasthttp
defer func() { <-inFlightSem }() defer func() { <-inFlightSem }()
default: default:
c.Error(fmt.Sprintf( response.Error(c, fmt.Sprintf(
"Limit of concurrent requests reached (%d), try again later.", opts.MaxRequestsInFlight, "Limit of concurrent requests reached (%d), try again later.", opts.MaxRequestsInFlight,
), fasthttp.StatusServiceUnavailable) ), fasthttp.StatusServiceUnavailable)
return return
@ -76,11 +77,11 @@ func metricsHandler(reg prometheus.Gatherer, opts promhttp.HandlerOpts) fasthttp
case promhttp.ContinueOnError: case promhttp.ContinueOnError:
if len(mfs) == 0 { if len(mfs) == 0 {
// Still report the error if no metrics have been gathered. // Still report the error if no metrics have been gathered.
c.Error(err.Error(), fasthttp.StatusServiceUnavailable) response.Error(c, err.Error(), fasthttp.StatusServiceUnavailable)
return return
} }
case promhttp.HTTPErrorOnError: case promhttp.HTTPErrorOnError:
c.Error(err.Error(), fasthttp.StatusServiceUnavailable) response.Error(c, err.Error(), fasthttp.StatusServiceUnavailable)
return return
} }
} }
@ -106,7 +107,7 @@ func metricsHandler(reg prometheus.Gatherer, opts promhttp.HandlerOpts) fasthttp
case promhttp.PanicOnError: case promhttp.PanicOnError:
panic(err) panic(err)
case promhttp.HTTPErrorOnError: case promhttp.HTTPErrorOnError:
c.Error(err.Error(), fasthttp.StatusServiceUnavailable) response.Error(c, err.Error(), fasthttp.StatusServiceUnavailable)
return true return true
default: default:
// Do nothing in all other cases, including ContinueOnError. // Do nothing in all other cases, including ContinueOnError.

View file

@ -5,6 +5,7 @@ import (
rtp "runtime/pprof" rtp "runtime/pprof"
"github.com/fasthttp/router" "github.com/fasthttp/router"
"github.com/nspcc-dev/neofs-http-gw/response"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/fasthttpadaptor" "github.com/valyala/fasthttp/fasthttpadaptor"
) )
@ -38,6 +39,6 @@ func pprofHandler() fasthttp.RequestHandler {
return return
} }
ctx.Error("Not found", fasthttp.StatusNotFound) response.Error(ctx, "Not found", fasthttp.StatusNotFound)
} }
} }

7
response/utils.go Normal file
View file

@ -0,0 +1,7 @@
package response
import "github.com/valyala/fasthttp"
func Error(r *fasthttp.RequestCtx, msg string, code int) {
r.Error(msg+"\n", code)
}

View file

@ -12,6 +12,7 @@ import (
"github.com/nspcc-dev/neofs-api-go/pkg/object" "github.com/nspcc-dev/neofs-api-go/pkg/object"
"github.com/nspcc-dev/neofs-api-go/pkg/owner" "github.com/nspcc-dev/neofs-api-go/pkg/owner"
"github.com/nspcc-dev/neofs-api-go/pkg/token" "github.com/nspcc-dev/neofs-api-go/pkg/token"
"github.com/nspcc-dev/neofs-http-gw/response"
"github.com/nspcc-dev/neofs-http-gw/tokens" "github.com/nspcc-dev/neofs-http-gw/tokens"
"github.com/nspcc-dev/neofs-sdk-go/pkg/pool" "github.com/nspcc-dev/neofs-sdk-go/pkg/pool"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
@ -51,12 +52,12 @@ func (u *Uploader) Upload(c *fasthttp.RequestCtx) {
) )
if err = tokens.StoreBearerToken(c); err != nil { if err = tokens.StoreBearerToken(c); err != nil {
log.Error("could not fetch bearer token", zap.Error(err)) log.Error("could not fetch bearer token", zap.Error(err))
c.Error("could not fetch bearer token", fasthttp.StatusBadRequest) response.Error(c, "could not fetch bearer token", fasthttp.StatusBadRequest)
return return
} }
if err = cid.Parse(scid); err != nil { if err = cid.Parse(scid); err != nil {
log.Error("wrong container id", zap.Error(err)) log.Error("wrong container id", zap.Error(err))
c.Error("wrong container id", fasthttp.StatusBadRequest) response.Error(c, "wrong container id", fasthttp.StatusBadRequest)
return return
} }
defer func() { defer func() {
@ -75,7 +76,7 @@ func (u *Uploader) Upload(c *fasthttp.RequestCtx) {
boundary := string(c.Request.Header.MultipartFormBoundary()) boundary := string(c.Request.Header.MultipartFormBoundary())
if file, err = fetchMultipartFile(u.log, bodyStream, boundary); err != nil { if file, err = fetchMultipartFile(u.log, bodyStream, boundary); err != nil {
log.Error("could not receive multipart/form", zap.Error(err)) log.Error("could not receive multipart/form", zap.Error(err))
c.Error("could not receive multipart/form: "+err.Error(), fasthttp.StatusBadRequest) response.Error(c, "could not receive multipart/form: "+err.Error(), fasthttp.StatusBadRequest)
return return
} }
filtered := filterHeaders(u.log, &c.Request.Header) filtered := filterHeaders(u.log, &c.Request.Header)
@ -112,7 +113,7 @@ func (u *Uploader) Upload(c *fasthttp.RequestCtx) {
if obj, err = u.pool.PutObject(c, ops, client.WithBearer(bt)); err != nil { if obj, err = u.pool.PutObject(c, ops, client.WithBearer(bt)); err != nil {
log.Error("could not store file in neofs", zap.Error(err)) log.Error("could not store file in neofs", zap.Error(err))
c.Error("could not store file in neofs", fasthttp.StatusBadRequest) response.Error(c, "could not store file in neofs", fasthttp.StatusBadRequest)
return return
} }
@ -122,7 +123,7 @@ func (u *Uploader) Upload(c *fasthttp.RequestCtx) {
// Try to return the response, otherwise, if something went wrong, throw an error. // Try to return the response, otherwise, if something went wrong, throw an error.
if err = newPutResponse(addr).encode(c); err != nil { if err = newPutResponse(addr).encode(c); err != nil {
log.Error("could not prepare response", zap.Error(err)) log.Error("could not prepare response", zap.Error(err))
c.Error("could not prepare response", fasthttp.StatusBadRequest) response.Error(c, "could not prepare response", fasthttp.StatusBadRequest)
return return
} }