fix: codeowner file parse error as first class error (#2003)

This commit is contained in:
Abhinav Singh 2024-04-24 19:53:21 +00:00 committed by Harness
parent e2689a3b40
commit e528ce2370
3 changed files with 27 additions and 16 deletions

View File

@ -18,7 +18,6 @@ import (
"context" "context"
"fmt" "fmt"
"github.com/harness/gitness/app/api/usererror"
"github.com/harness/gitness/app/auth" "github.com/harness/gitness/app/auth"
"github.com/harness/gitness/app/services/codeowners" "github.com/harness/gitness/app/services/codeowners"
"github.com/harness/gitness/types" "github.com/harness/gitness/types"
@ -46,9 +45,7 @@ func (c *Controller) CodeOwners(
} }
ownerEvaluation, err := c.codeOwners.Evaluate(ctx, repo, pr, reviewers) ownerEvaluation, err := c.codeOwners.Evaluate(ctx, repo, pr, reviewers)
if codeowners.IsTooLargeError(err) {
return types.CodeOwnerEvaluation{}, usererror.UnprocessableEntityf(err.Error())
}
if err != nil { if err != nil {
return types.CodeOwnerEvaluation{}, err return types.CodeOwnerEvaluation{}, err
} }

View File

@ -33,12 +33,13 @@ import (
func Translate(ctx context.Context, err error) *Error { func Translate(ctx context.Context, err error) *Error {
var ( var (
rError *Error rError *Error
checkError *check.ValidationError checkError *check.ValidationError
appError *errors.Error appError *errors.Error
maxBytesErr *http.MaxBytesError maxBytesErr *http.MaxBytesError
codeOwnersTooLargeError *codeowners.TooLargeError codeOwnersTooLargeError *codeowners.TooLargeError
lockError *lock.Error codeOwnersFileParseError *codeowners.FileParseError
lockError *lock.Error
) )
// print original error for debugging purposes // print original error for debugging purposes
@ -106,7 +107,8 @@ func Translate(ctx context.Context, err error) *Error {
return ErrCodeOwnersNotFound return ErrCodeOwnersNotFound
case errors.As(err, &codeOwnersTooLargeError): case errors.As(err, &codeOwnersTooLargeError):
return UnprocessableEntityf(codeOwnersTooLargeError.Error()) return UnprocessableEntityf(codeOwnersTooLargeError.Error())
case errors.As(err, &codeOwnersFileParseError):
return UnprocessableEntityf(codeOwnersFileParseError.Error())
// lock errors // lock errors
case errors.As(err, &lockError): case errors.As(err, &lockError):
return errorFromLockError(lockError) return errorFromLockError(lockError)

View File

@ -51,10 +51,6 @@ type TooLargeError struct {
FileSize int64 FileSize int64
} }
func IsTooLargeError(err error) bool {
return errors.Is(err, &TooLargeError{})
}
func (e *TooLargeError) Error() string { func (e *TooLargeError) Error() string {
return fmt.Sprintf( return fmt.Sprintf(
"The repository's CODEOWNERS file size %.2fMB exceeds the maximum supported size of %dMB", "The repository's CODEOWNERS file size %.2fMB exceeds the maximum supported size of %dMB",
@ -69,6 +65,22 @@ func (e *TooLargeError) Is(target error) bool {
return ok return ok
} }
// FileParseError represents an error if codeowners file is not parsable.
type FileParseError struct {
line string
}
func (e *FileParseError) Error() string {
return fmt.Sprintf(
"The repository's CODEOWNERS file has an invalid line: %s", e.line,
)
}
func (e *FileParseError) Is(target error) bool {
_, ok := target.(*FileParseError)
return ok
}
type Config struct { type Config struct {
FilePaths []string FilePaths []string
} }
@ -173,7 +185,7 @@ func (s *Service) parseCodeOwner(codeOwnersContent string) ([]Entry, error) {
parts := strings.Split(line, " ") parts := strings.Split(line, " ")
if len(parts) < 2 { if len(parts) < 2 {
return nil, fmt.Errorf("line has invalid format: '%s'", line) return nil, &FileParseError{line}
} }
pattern := parts[0] pattern := parts[0]