Skip to content

Commit

Permalink
Handle more errors
Browse files Browse the repository at this point in the history
  • Loading branch information
prymitive committed Mar 7, 2025
1 parent da7f192 commit e1fc0a3
Show file tree
Hide file tree
Showing 10 changed files with 448 additions and 138 deletions.
5 changes: 3 additions & 2 deletions cmd/pint/tests/0122_lint_owner_allowed.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ Bug: missing owner (rule/owner)
5 | expr: up > 0
^^^ `rule/owner` comments are required in all files, please add a `# pint file/owner $owner` somewhere in this file and/or `# pint rule/owner $owner` on top of each rule.

Bug: This file is set as owned by `ax` but `ax` doesn't match any of the allowed owner values. (rule/owner)
Bug: invalid owner (rule/owner)
---> rules/3.yml:1
1 | # pint file/owner ax
1 | # pint file/owner ax
^^ This file is set as owned by `ax` but `ax` doesn't match any of the allowed owner values.

level=INFO msg="Problems found" Bug=5
level=ERROR msg="Execution completed with error(s)" err="found 1 problem(s) with severity Bug or higher"
Expand Down
8 changes: 5 additions & 3 deletions cmd/pint/tests/0165_pint_comment_error.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ cmp stderr stderr.txt
-- stderr.txt --
level=INFO msg="Finding all rules to check" paths=["rules"]
level=INFO msg="Checking Prometheus rules" entries=3 workers=10 online=true
Warning: This comment is not a valid pint control comment: unexpected comment suffix: "this line" (pint/comment)
Warning: invalid comment (pint/comment)
---> rules/1.yml:4
4 | # pint ignore/line this line
4 | # pint ignore/line this line
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This comment is not a valid pint control comment: unexpected comment suffix: "this line"

Information: This file was excluded from pint checks. (ignore/file)
---> rules/2.yml:4
4 | # pint ignore/file
4 | # pint ignore/file
^^^^^^^^^^^^^^^^ This file was excluded from pint checks.

level=INFO msg="Problems found" Warning=1 Information=1
-- rules/1.yml --
Expand Down
48 changes: 27 additions & 21 deletions internal/checks/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,44 +70,50 @@ func parseRuleError(rule parser.Rule, err error) Problem {
return Problem{
Anchor: AnchorAfter,
Lines: diags.LineRange{
First: ignoreErr.Line,
Last: ignoreErr.Line,
First: ignoreErr.Diagnostic.Pos.Lines().First,
Last: ignoreErr.Diagnostic.Pos.Lines().Last,
},
Reporter: ignoreFileReporter,
Summary: ignoreErr.Error(),
Details: "checks disabled",
Severity: Information,
Diagnostics: []diags.Diagnostic{
ignoreErr.Diagnostic,
},
Reporter: ignoreFileReporter,
Summary: ignoreErr.Error(),
Details: "",
Severity: Information,
Diagnostics: nil, // FIXME needs Pos & columns
}

case errors.As(err, &commentErr):
slog.Debug("invalid comment report", slog.Any("err", commentErr))
return Problem{
Anchor: AnchorAfter,
Lines: diags.LineRange{
First: commentErr.Line,
Last: commentErr.Line,
First: commentErr.Diagnostic.Pos.Lines().First,
Last: commentErr.Diagnostic.Pos.Lines().Last,
},
Reporter: pintCommentReporter,
Summary: "invalid comment",
Details: "",
Severity: Warning,
Diagnostics: []diags.Diagnostic{
commentErr.Diagnostic,
},
Reporter: pintCommentReporter,
Summary: "This comment is not a valid pint control comment: " + commentErr.Error(),
Details: "",
Severity: Warning,
Diagnostics: nil, // FIXME needs Pos & columns
}

case errors.As(err, &ownerErr):
slog.Debug("invalid owner report", slog.Any("err", ownerErr))
return Problem{
Anchor: AnchorAfter,
Lines: diags.LineRange{
First: ownerErr.Line,
Last: ownerErr.Line,
First: ownerErr.Diagnostic.Pos.Lines().First,
Last: ownerErr.Diagnostic.Pos.Lines().Last,
},
Reporter: discovery.RuleOwnerComment,
Summary: "invalid owner",
Details: "",
Severity: Bug,
Diagnostics: []diags.Diagnostic{
ownerErr.Diagnostic,
},
Reporter: discovery.RuleOwnerComment,
Summary: fmt.Sprintf("This file is set as owned by `%s` but `%s` doesn't match any of the allowed owner values.", ownerErr.Name, ownerErr.Name),
Details: "",
Severity: Bug,
Diagnostics: nil, // FIXME needs Pos & columns
}

case errors.As(err, &parseErr):
Expand Down
47 changes: 29 additions & 18 deletions internal/comments/comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"strings"
"time"
"unicode"

"github.com/cloudflare/pint/internal/diags"
)

type Type uint8
Expand Down Expand Up @@ -86,21 +88,19 @@ func parseType(s string) Type {
}

type CommentError struct {
Err error
Line int
Diagnostic diags.Diagnostic
}

func (ce CommentError) Error() string {
return ce.Err.Error()
return ce.Diagnostic.Message
}

type OwnerError struct {
Name string
Line int
Diagnostic diags.Diagnostic
}

func (oe OwnerError) Error() string {
return oe.Name
return oe.Diagnostic.Message
}

type Invalid struct {
Expand Down Expand Up @@ -219,7 +219,8 @@ const (
readsValue
)

func parseComment(s string, line int) (parsed []Comment, err error) {
func parseComment(s string, line int) (parsed []Comment) {
var err error
var buf strings.Builder
var c Comment

Expand Down Expand Up @@ -302,27 +303,37 @@ func parseComment(s string, line int) (parsed []Comment, err error) {

if c.Type != UnknownType {
c.Value, err = parseValue(c.Type, strings.TrimSpace(buf.String()), line)
if err != nil {
c.Type = InvalidComment
c.Value = Invalid{
Err: CommentError{
Diagnostic: diags.Diagnostic{
Message: "This comment is not a valid pint control comment: " + err.Error(),
Pos: diags.PositionRanges{
{
Line: line,
FirstColumn: c.Offset + 1,
LastColumn: len(s),
},
},
FirstColumn: 1,
LastColumn: len(s),
},
},
}
}
parsed = append(parsed, c)
}

return parsed, err
return parsed
}

func Parse(lineno int, text string) (comments []Comment) {
sc := bufio.NewScanner(strings.NewReader(text))
var index int
for sc.Scan() {
line := sc.Text()
parsed, err := parseComment(line, lineno+index)
if err != nil {
comments = append(comments, Comment{
Type: InvalidComment,
Value: Invalid{Err: CommentError{Line: lineno + index, Err: err}},
Offset: 0,
})
continue
}
comments = append(comments, parsed...)
comments = append(comments, parseComment(line, lineno+index)...)
index++
}
return comments
Expand Down
Loading

0 comments on commit e1fc0a3

Please sign in to comment.