Skip to content

Commit

Permalink
handler: Return http code according to the NeoFS status
Browse files Browse the repository at this point in the history
Signed-off-by: Evgenii Baidakov <evgenii@nspcc.io>
  • Loading branch information
smallhive committed Oct 23, 2024
1 parent edd9220 commit cd5da44
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
6 changes: 3 additions & 3 deletions handlers/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (a *RestAPI) PutContainer(ctx echo.Context, params apiserver.PutContainerPa
cnrID, err := createContainer(wCtx, a.pool, stoken, body, params, a.signer, a.networkInfoGetter)
if err != nil {
resp := a.logAndGetErrorResponse("create container", err)
return ctx.JSON(http.StatusBadRequest, resp)
return ctx.JSON(getResponseCodeFromStatus(err), resp)
}

resp := apiserver.PutContainerOK{
Expand Down Expand Up @@ -144,7 +144,7 @@ func (a *RestAPI) PutContainerEACL(ctx echo.Context, containerID apiserver.Conta

if err = setContainerEACL(wCtx, a.pool, cnrID, stoken, body, a.signer); err != nil {
resp := a.logAndGetErrorResponse("failed set container eacl", err)
return ctx.JSON(http.StatusBadRequest, resp)
return ctx.JSON(getResponseCodeFromStatus(err), resp)
}

ctx.Response().Header().Set(accessControlAllowOriginHeader, "*")
Expand Down Expand Up @@ -266,7 +266,7 @@ func (a *RestAPI) DeleteContainer(ctx echo.Context, containerID apiserver.Contai

if err = wait.ContainerDelete(wCtx, cnrID, a.signer, prm); err != nil {
resp := a.logAndGetErrorResponse("delete container", err, zap.String("container", containerID))
return ctx.JSON(http.StatusBadRequest, resp)
return ctx.JSON(getResponseCodeFromStatus(err), resp)
}

ctx.Response().Header().Set(accessControlAllowOriginHeader, "*")
Expand Down
50 changes: 50 additions & 0 deletions handlers/conv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package handlers

import (
"errors"
"net/http"

apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status"
)

func getResponseCodeFromStatus(err error) int {
if err == nil {
return http.StatusOK
}

switch {
case errors.Is(err, apistatus.ErrEACLNotFound):
return http.StatusNotFound
case errors.Is(err, apistatus.ErrContainerNotFound):
return http.StatusNotFound
case errors.Is(err, apistatus.ErrServerInternal):
return http.StatusInternalServerError
case errors.Is(err, apistatus.ErrSessionTokenNotFound):
return http.StatusNotFound
case errors.Is(err, apistatus.ErrSessionTokenExpired):
return http.StatusInternalServerError
case errors.Is(err, apistatus.ErrObjectLocked):
return http.StatusForbidden
case errors.Is(err, apistatus.ErrObjectAlreadyRemoved):
return http.StatusInternalServerError
case errors.Is(err, apistatus.ErrLockNonRegularObject):
return http.StatusInternalServerError
case errors.Is(err, apistatus.ErrObjectAccessDenied):
return http.StatusForbidden
case errors.Is(err, apistatus.ErrObjectNotFound):
return http.StatusNotFound
case errors.Is(err, apistatus.ErrObjectOutOfRange):
return http.StatusNotFound
case errors.Is(err, apistatus.ErrServerInternal):
return http.StatusInternalServerError
case errors.Is(err, apistatus.ErrWrongMagicNumber):
return http.StatusNotFound
case errors.Is(err, apistatus.ErrSignatureVerification):
return http.StatusForbidden
case errors.Is(err, apistatus.ErrNodeUnderMaintenance):
return http.StatusInternalServerError

default:
return http.StatusInternalServerError
}
}

0 comments on commit cd5da44

Please sign in to comment.