diff --git a/app/api/controller/pullreq/codeowner.go b/app/api/controller/pullreq/codeowner.go index 097b39982..d05ff6b5e 100644 --- a/app/api/controller/pullreq/codeowner.go +++ b/app/api/controller/pullreq/codeowner.go @@ -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 } diff --git a/app/api/usererror/translate.go b/app/api/usererror/translate.go index 39b17eb06..8aaeca03c 100644 --- a/app/api/usererror/translate.go +++ b/app/api/usererror/translate.go @@ -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) diff --git a/app/services/codeowners/service.go b/app/services/codeowners/service.go index d5f94a351..b49458047 100644 --- a/app/services/codeowners/service.go +++ b/app/services/codeowners/service.go @@ -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]