forked from TrueCloudLab/frostfs-http-gw
[#187] Fix handling ErrAccessDenied and ErrGatewayTimeout
If an Access Denied error occurs, we should return 403 Forbidden instead of 400 Bad Request. Gateway Timeout error we also need to process and return the 504 Gateway Timeout code. Signed-off-by: Roman Loginov <r.loginov@yadro.com>
This commit is contained in:
parent
a4e3767d4b
commit
ce04b0836a
8 changed files with 132 additions and 82 deletions
|
@ -2,11 +2,12 @@ package handler
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/internal/logs"
|
||||
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/response"
|
||||
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/tokens"
|
||||
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/bearer"
|
||||
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client"
|
||||
|
@ -27,11 +28,10 @@ func (r *request) handleFrostFSErr(err error, start time.Time) {
|
|||
zap.Stringer("elapsed", time.Since(start)),
|
||||
zap.Error(err),
|
||||
}
|
||||
statusCode, msg, additionalFields := response.FormErrorResponse("could not receive object", err)
|
||||
logFields = append(logFields, additionalFields...)
|
||||
statusCode, msg := formErrorResponse("could not receive object", err)
|
||||
|
||||
r.log.Error(logs.CouldNotReceiveObject, logFields...)
|
||||
response.Error(r.RequestCtx, msg, statusCode)
|
||||
ResponseError(r.RequestCtx, msg, statusCode)
|
||||
}
|
||||
|
||||
func bearerToken(ctx context.Context) *bearer.Token {
|
||||
|
@ -76,13 +76,10 @@ func isValidValue(s string) bool {
|
|||
}
|
||||
|
||||
func logAndSendBucketError(c *fasthttp.RequestCtx, log *zap.Logger, err error) {
|
||||
log.Error(logs.CouldntGetBucket, zap.Error(err))
|
||||
statusCode, msg := formErrorResponse("could not get bucket", err)
|
||||
|
||||
if client.IsErrContainerNotFound(err) {
|
||||
response.Error(c, "Not Found", fasthttp.StatusNotFound)
|
||||
return
|
||||
}
|
||||
response.Error(c, "could not get bucket: "+err.Error(), fasthttp.StatusBadRequest)
|
||||
log.Error(logs.CouldntGetBucket, zap.Error(err))
|
||||
ResponseError(c, msg, statusCode)
|
||||
}
|
||||
|
||||
func newAddress(cnr cid.ID, obj oid.ID) oid.Address {
|
||||
|
@ -91,3 +88,31 @@ func newAddress(cnr cid.ID, obj oid.ID) oid.Address {
|
|||
addr.SetObject(obj)
|
||||
return addr
|
||||
}
|
||||
|
||||
func ResponseError(r *fasthttp.RequestCtx, msg string, code int) {
|
||||
r.Error(msg+"\n", code)
|
||||
}
|
||||
|
||||
func formErrorResponse(message string, err error) (int, string) {
|
||||
var (
|
||||
errMsg string
|
||||
statusCode int
|
||||
)
|
||||
|
||||
switch {
|
||||
case errors.Is(err, ErrAccessDenied):
|
||||
statusCode = fasthttp.StatusForbidden
|
||||
errMsg = err.Error()
|
||||
case errors.Is(err, ErrGatewayTimeout):
|
||||
statusCode = fasthttp.StatusBadGateway
|
||||
errMsg = err.Error()
|
||||
case client.IsErrObjectNotFound(err) || client.IsErrContainerNotFound(err):
|
||||
statusCode = fasthttp.StatusNotFound
|
||||
errMsg = "Not Found"
|
||||
default:
|
||||
statusCode = fasthttp.StatusBadRequest
|
||||
errMsg = err.Error()
|
||||
}
|
||||
|
||||
return statusCode, fmt.Sprintf("%s: %s", message, errMsg)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue