HttpAPIErrorMessage intends to enforce constructing proper sentences as error messages for the api. A proper sentence includes proper capitalization and ends with punctuation. There are ways around the linter, but this should work in the common cases.
(m dsl.Matcher)
| 253 | // and ends with punctuation. |
| 254 | // There are ways around the linter, but this should work in the common cases. |
| 255 | func HttpAPIErrorMessage(m dsl.Matcher) { |
| 256 | m.Import("github.com/coder/coder/v2/coderd/httpapi") |
| 257 | |
| 258 | isNotProperError := func(v dsl.Var) bool { |
| 259 | return v.Type.Is("string") && |
| 260 | // Either starts with a lowercase, or ends without punctuation. |
| 261 | // The reason I don't check for NOT ^[A-Z].*[.!?]$ is because there |
| 262 | // are some exceptions. Any string starting with a formatting |
| 263 | // directive (%s) for example is exempt. |
| 264 | (m["m"].Text.Matches(`^"[a-z].*`) || |
| 265 | m["m"].Text.Matches(`.*[^.!?]"$`)) |
| 266 | } |
| 267 | |
| 268 | m.Match(` |
| 269 | httpapi.Write($_, $_, $s, httpapi.Response{ |
| 270 | $*_, |
| 271 | Message: $m, |
| 272 | $*_, |
| 273 | }) |
| 274 | `, ` |
| 275 | httpapi.Write($_, $_, $s, httpapi.Response{ |
| 276 | $*_, |
| 277 | Message: fmt.$f($m, $*_), |
| 278 | $*_, |
| 279 | }) |
| 280 | `, |
| 281 | ).Where(isNotProperError(m["m"])). |
| 282 | At(m["m"]). |
| 283 | Report("Field \"Message\" should be a proper sentence with a capitalized first letter and ending in punctuation. $m") |
| 284 | } |
| 285 | |
| 286 | // HttpAPIReturn will report a linter violation if the http function is not |
| 287 | // returned after writing a response to the client. |