From 98a18210a113148a237e25464e0b031f699d321f Mon Sep 17 00:00:00 2001 From: Enver Bisevac Date: Mon, 18 Dec 2023 14:29:09 +0000 Subject: [PATCH] [maint] in when tree entry is not found return PathNotFound error (#921) --- app/api/controller/pullreq/comment_create.go | 3 ++- app/api/controller/pullreq/file_view_add.go | 6 +++--- app/api/usererror/translate.go | 7 ++++++- app/services/codeowners/service.go | 3 ++- errors/status.go | 1 - git/adapter/tree.go | 2 +- git/types/errors.go | 12 +++++------- 7 files changed, 19 insertions(+), 15 deletions(-) diff --git a/app/api/controller/pullreq/comment_create.go b/app/api/controller/pullreq/comment_create.go index 3972b081e..56c19fedd 100644 --- a/app/api/controller/pullreq/comment_create.go +++ b/app/api/controller/pullreq/comment_create.go @@ -25,6 +25,7 @@ import ( events "github.com/harness/gitness/app/events/pullreq" "github.com/harness/gitness/errors" "github.com/harness/gitness/git" + gittypes "github.com/harness/gitness/git/types" "github.com/harness/gitness/store" "github.com/harness/gitness/types" "github.com/harness/gitness/types/enum" @@ -343,7 +344,7 @@ func (c *Controller) fetchDiffCut( LineEnd: in.LineEnd, LineEndNew: in.LineEndNew, }) - if errors.AsStatus(err) == errors.StatusNotFound || errors.AsStatus(err) == errors.StatusPathNotFound { + if errors.AsStatus(err) == errors.StatusNotFound || gittypes.IsPathNotFoundError(err) { return git.DiffCutOutput{}, usererror.BadRequest(errors.Message(err)) } if err != nil { diff --git a/app/api/controller/pullreq/file_view_add.go b/app/api/controller/pullreq/file_view_add.go index 1f842762b..bd4262024 100644 --- a/app/api/controller/pullreq/file_view_add.go +++ b/app/api/controller/pullreq/file_view_add.go @@ -21,8 +21,8 @@ import ( "github.com/harness/gitness/app/api/usererror" "github.com/harness/gitness/app/auth" - "github.com/harness/gitness/errors" "github.com/harness/gitness/git" + gittypes "github.com/harness/gitness/git/types" "github.com/harness/gitness/types" "github.com/harness/gitness/types/enum" ) @@ -80,7 +80,7 @@ func (c *Controller) FileViewAdd( Path: in.Path, IncludeLatestCommit: false, }) - if err != nil && errors.AsStatus(err) != errors.StatusPathNotFound { + if err != nil && !gittypes.IsPathNotFoundError(err) { return nil, fmt.Errorf( "failed to get tree node '%s' for provided sha '%s': %w", in.Path, @@ -102,7 +102,7 @@ func (c *Controller) FileViewAdd( Path: in.Path, IncludeLatestCommit: false, }) - if err != nil && errors.AsStatus(err) != errors.StatusPathNotFound { + if err != nil && !gittypes.IsPathNotFoundError(err) { return nil, fmt.Errorf( "failed to get tree node '%s' for MergeBaseSHA '%s': %w", in.Path, diff --git a/app/api/usererror/translate.go b/app/api/usererror/translate.go index ab3c96768..5cf0927b7 100644 --- a/app/api/usererror/translate.go +++ b/app/api/usererror/translate.go @@ -22,6 +22,7 @@ import ( "github.com/harness/gitness/app/services/webhook" "github.com/harness/gitness/blob" "github.com/harness/gitness/errors" + gittypes "github.com/harness/gitness/git/types" "github.com/harness/gitness/lock" "github.com/harness/gitness/store" "github.com/harness/gitness/types/check" @@ -79,6 +80,11 @@ func Translate(err error) *Error { return RequestTooLargef("The request is too large. maximum allowed size is %d bytes", maxBytesErr.Limit) // git errors + case errors.Is(err, &gittypes.PathNotFoundError{}): + return &Error{ + Status: http.StatusNotFound, + Message: err.Error(), + } case errors.As(err, &appError): return NewWithPayload(httpStatusCode( appError.Status), @@ -124,7 +130,6 @@ var codes = map[errors.Status]int{ errors.StatusConflict: http.StatusConflict, errors.StatusInvalidArgument: http.StatusBadRequest, errors.StatusNotFound: http.StatusNotFound, - errors.StatusPathNotFound: http.StatusNotFound, errors.StatusNotImplemented: http.StatusNotImplemented, errors.StatusPreconditionFailed: http.StatusPreconditionFailed, errors.StatusUnauthorized: http.StatusUnauthorized, diff --git a/app/services/codeowners/service.go b/app/services/codeowners/service.go index f07559129..823d3fcdd 100644 --- a/app/services/codeowners/service.go +++ b/app/services/codeowners/service.go @@ -25,6 +25,7 @@ import ( "github.com/harness/gitness/app/store" "github.com/harness/gitness/errors" "github.com/harness/gitness/git" + gittypes "github.com/harness/gitness/git/types" gitness_store "github.com/harness/gitness/store" "github.com/harness/gitness/types" "github.com/harness/gitness/types/enum" @@ -255,7 +256,7 @@ func (s *Service) getCodeOwnerFileNode( Path: path, }) - if errors.AsStatus(err) == errors.StatusPathNotFound { + if gittypes.IsPathNotFoundError(err) { continue } if err != nil { diff --git a/errors/status.go b/errors/status.go index 4ca0bc4b3..cda3e4101 100644 --- a/errors/status.go +++ b/errors/status.go @@ -26,7 +26,6 @@ const ( StatusInternal Status = "internal" StatusInvalidArgument Status = "invalid" StatusNotFound Status = "not_found" - StatusPathNotFound Status = "path_not_found" StatusNotImplemented Status = "not_implemented" StatusUnauthorized Status = "unauthorized" StatusFailed Status = "failed" diff --git a/git/adapter/tree.go b/git/adapter/tree.go index edb0fc57a..2f34b793e 100644 --- a/git/adapter/tree.go +++ b/git/adapter/tree.go @@ -87,7 +87,7 @@ func lsTree( } if output == "" { - return nil, errors.Format(errors.StatusPathNotFound, "path %q not found", treePath) + return nil, &types.PathNotFoundError{Path: treePath} } n := strings.Count(output, "\x00") diff --git a/git/types/errors.go b/git/types/errors.go index 713309f88..21b700f6d 100644 --- a/git/types/errors.go +++ b/git/types/errors.go @@ -26,13 +26,11 @@ const ( ) var ( - ErrAlreadyExists = errors.New("already exists") - ErrRepositoryNotFound = errors.New("repository not found") - ErrRepositoryCorrupted = errors.New("repository corrupted") - ErrInvalidPath = errors.New("path is invalid") - ErrSHADoesNotMatch = errors.New("sha does not match") - ErrNoDefaultBranch = errors.New("no default branch") - ErrHunkNotFound = errors.New("hunk not found") + ErrAlreadyExists = errors.New("already exists") + ErrInvalidPath = errors.New("path is invalid") + ErrSHADoesNotMatch = errors.New("sha does not match") + ErrNoDefaultBranch = errors.New("no default branch") + ErrHunkNotFound = errors.New("hunk not found") ) type NotFoundError struct {