diff --git a/handlers/containers.go b/handlers/containers.go index 7e61a20..f0f19d7 100644 --- a/handlers/containers.go +++ b/handlers/containers.go @@ -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{ @@ -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, "*") @@ -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, "*") diff --git a/handlers/conv.go b/handlers/conv.go new file mode 100644 index 0000000..4590f04 --- /dev/null +++ b/handlers/conv.go @@ -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 + } +}