From 49d6a2756291452ad212bcaf4f96f165741ed41f Mon Sep 17 00:00:00 2001 From: Denis Kirillov Date: Wed, 4 Oct 2023 15:39:44 +0300 Subject: [PATCH] [#70] Adjust status codes Signed-off-by: Denis Kirillov --- internal/handler/handler.go | 30 +++++++++++------------------- internal/logs/logs.go | 2 +- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/internal/handler/handler.go b/internal/handler/handler.go index 54602c2..fa3c364 100644 --- a/internal/handler/handler.go +++ b/internal/handler/handler.go @@ -55,17 +55,6 @@ func New(params *utils.AppParams, config Config, tree *tree.Tree) *Handler { } } -// getContainerID decode container id, if it's not a valid container id -// then trey to resolve name using provided resolver. -func (h *Handler) getContainerID(ctx context.Context, containerID string) (*cid.ID, error) { - cnrID := new(cid.ID) - err := cnrID.DecodeString(containerID) - if err != nil { - cnrID, err = h.containerResolver.Resolve(ctx, containerID) - } - return cnrID, err -} - // byAddress is a wrapper for function (e.g. request.headObject, request.receiveFile) that // prepares request and object address to it. func (h *Handler) byAddress(c *fasthttp.RequestCtx, f func(context.Context, request, oid.Address)) { @@ -116,7 +105,12 @@ func (h *Handler) byObjectName(req *fasthttp.RequestCtx, f func(context.Context, foundOid, err := h.tree.GetLatestVersion(ctx, &bktInfo.CID, key) if err != nil { - log.Error(logs.ObjectWasntFound, zap.Error(err)) + if errors.Is(err, tree.ErrNodeAccessDenied) { + response.Error(req, "Access Denied", fasthttp.StatusForbidden) + return + } + log.Error(logs.GetLatestObjectVersion, zap.Error(err)) + response.Error(req, "object wasn't found", fasthttp.StatusNotFound) return } @@ -144,14 +138,13 @@ func (h *Handler) byAttribute(c *fasthttp.RequestCtx, f func(context.Context, re ctx := utils.GetContextFromRequest(c) - containerID, err := h.getContainerID(ctx, scid) + bktInfo, err := h.getBucketInfo(ctx, scid, log) if err != nil { - log.Error(logs.WrongContainerID, zap.Error(err)) - response.Error(c, "wrong container id", fasthttp.StatusBadRequest) + logAndSendBucketError(c, log, err) return } - res, err := h.search(ctx, containerID, key, val, object.MatchStringEqual) + res, err := h.search(ctx, &bktInfo.CID, key, val, object.MatchStringEqual) if err != nil { log.Error(logs.CouldNotSearchForObjects, zap.Error(err)) response.Error(c, "could not search for objects: "+err.Error(), fasthttp.StatusBadRequest) @@ -176,7 +169,7 @@ func (h *Handler) byAttribute(c *fasthttp.RequestCtx, f func(context.Context, re } var addrObj oid.Address - addrObj.SetContainer(*containerID) + addrObj.SetContainer(bktInfo.CID) addrObj.SetObject(buf[0]) f(ctx, *h.newRequest(c, log), addrObj) @@ -190,8 +183,7 @@ func (h *Handler) resolveContainer(ctx context.Context, containerID string) (*ci if err != nil { cnrID, err = h.containerResolver.Resolve(ctx, containerID) if err != nil && strings.Contains(err.Error(), "not found") { - err = fmt.Errorf("%w: %s", &apistatus.ContainerNotFound{}, err.Error()) - + err = fmt.Errorf("%w: %s", new(apistatus.ContainerNotFound), err.Error()) } } return cnrID, err diff --git a/internal/logs/logs.go b/internal/logs/logs.go index 79ddce5..0534ebc 100644 --- a/internal/logs/logs.go +++ b/internal/logs/logs.go @@ -6,7 +6,7 @@ const ( CouldNotReceiveObject = "could not receive object" // Error in ../../downloader/download.go WrongContainerID = "wrong container id" // Error in ../../downloader/download.go and uploader/upload.go WrongObjectID = "wrong object id" // Error in ../../downloader/download.go - ObjectWasntFound = "object wasn't found" // Error in ../../downloader/download.go + GetLatestObjectVersion = "get latest object version" // Error in ../../downloader/download.go ObjectWasDeleted = "object was deleted" // Error in ../../downloader/download.go CouldNotSearchForObjects = "could not search for objects" // Error in ../../downloader/download.go ObjectNotFound = "object not found" // Error in ../../downloader/download.go