Skip to content

Commit 6970d2b

Browse files
committed
chore: Linter rule for properly formatted api errors
1 parent 3e419dd commit 6970d2b

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

scripts/rules.go

+32
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,35 @@ func InTx(m dsl.Matcher) {
115115
`).Where(m["x"].Text != m["y"].Text).
116116
At(m["f"]).Report("Pass the tx database into the '$f' function inside the closure. Use '$y' over $x'")
117117
}
118+
119+
// HttpAPIErrorMessage intends to enforce constructing proper sentences as
120+
// error messages for the api. A proper sentence includes proper capitalization
121+
// and ends with punctuation.
122+
// There are ways around the linter, but this should work in the common cases.
123+
func HttpAPIErrorMessage(m dsl.Matcher) {
124+
m.Import("github.com/coder/coder/coderd/httpapi")
125+
126+
isNotProperError := func(v dsl.Var) bool {
127+
return v.Type.Is("string") &&
128+
// Either starts with a lowercase, or ends without punctuation
129+
(m["m"].Text.Matches(`^"[a-z].*`) ||
130+
m["m"].Text.Matches(`.*[^.!?]"$`))
131+
}
132+
133+
m.Match(`
134+
httpapi.Write($_, $s, httpapi.Response{
135+
$*_,
136+
Message: $m,
137+
$*_,
138+
})
139+
`, `
140+
httpapi.Write($_, $s, httpapi.Response{
141+
$*_,
142+
Message: fmt.$f($m, $*_),
143+
$*_,
144+
})
145+
`,
146+
).Where(isNotProperError(m["m"])).
147+
At(m["m"]).
148+
Report("Field \"Message\" should be a proper sentence with a capitalized first letter and ending in punctuation. $m")
149+
}

0 commit comments

Comments
 (0)