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

View File

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

View File

@ -51,10 +51,6 @@ type TooLargeError struct {
FileSize int64
}
func IsTooLargeError(err error) bool {
return errors.Is(err, &TooLargeError{})
}
func (e *TooLargeError) Error() string {
return fmt.Sprintf(
"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
}
// 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 {
FilePaths []string
}
@ -173,7 +185,7 @@ func (s *Service) parseCodeOwner(codeOwnersContent string) ([]Entry, error) {
parts := strings.Split(line, " ")
if len(parts) < 2 {
return nil, fmt.Errorf("line has invalid format: '%s'", line)
return nil, &FileParseError{line}
}
pattern := parts[0]