From a29c35f5f64de29fceede780b7041a10a72ff6b8 Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Sun, 10 Dec 2023 11:03:23 +0100 Subject: [PATCH] enable errorlint linter Signed-off-by: Matthieu MOREL --- .golangci.yml | 1 + config/http_config.go | 16 ++++++++-------- expfmt/bench_test.go | 7 ++++--- expfmt/decode_test.go | 7 ++++--- expfmt/text_parse.go | 5 +++-- expfmt/text_parse_test.go | 2 +- model/alert.go | 4 ++-- model/silence.go | 2 +- model/value.go | 2 +- 9 files changed, 25 insertions(+), 21 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 1711d51d..03640f20 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,5 +1,6 @@ linters: enable: + - errorlint - misspell - revive diff --git a/config/http_config.go b/config/http_config.go index 14ee6738..4a926e8d 100644 --- a/config/http_config.go +++ b/config/http_config.go @@ -630,7 +630,7 @@ func (rt *authorizationCredentialsFileRoundTripper) RoundTrip(req *http.Request) if len(req.Header.Get("Authorization")) == 0 { b, err := os.ReadFile(rt.authCredentialsFile) if err != nil { - return nil, fmt.Errorf("unable to read authorization credentials file %s: %s", rt.authCredentialsFile, err) + return nil, fmt.Errorf("unable to read authorization credentials file %s: %w", rt.authCredentialsFile, err) } authCredentials := strings.TrimSpace(string(b)) @@ -670,7 +670,7 @@ func (rt *basicAuthRoundTripper) RoundTrip(req *http.Request) (*http.Response, e if rt.usernameFile != "" { usernameBytes, err := os.ReadFile(rt.usernameFile) if err != nil { - return nil, fmt.Errorf("unable to read basic auth username file %s: %s", rt.usernameFile, err) + return nil, fmt.Errorf("unable to read basic auth username file %s: %w", rt.usernameFile, err) } username = strings.TrimSpace(string(usernameBytes)) } else { @@ -679,7 +679,7 @@ func (rt *basicAuthRoundTripper) RoundTrip(req *http.Request) (*http.Response, e if rt.passwordFile != "" { passwordBytes, err := os.ReadFile(rt.passwordFile) if err != nil { - return nil, fmt.Errorf("unable to read basic auth password file %s: %s", rt.passwordFile, err) + return nil, fmt.Errorf("unable to read basic auth password file %s: %w", rt.passwordFile, err) } password = strings.TrimSpace(string(passwordBytes)) } else { @@ -723,7 +723,7 @@ func (rt *oauth2RoundTripper) RoundTrip(req *http.Request) (*http.Response, erro if rt.config.ClientSecretFile != "" { data, err := os.ReadFile(rt.config.ClientSecretFile) if err != nil { - return nil, fmt.Errorf("unable to read oauth2 client secret file %s: %s", rt.config.ClientSecretFile, err) + return nil, fmt.Errorf("unable to read oauth2 client secret file %s: %w", rt.config.ClientSecretFile, err) } secret = strings.TrimSpace(string(data)) rt.mtx.RLock() @@ -977,7 +977,7 @@ func (c *TLSConfig) getClientCertificate(_ *tls.CertificateRequestInfo) (*tls.Ce if c.CertFile != "" { certData, err = os.ReadFile(c.CertFile) if err != nil { - return nil, fmt.Errorf("unable to read specified client cert (%s): %s", c.CertFile, err) + return nil, fmt.Errorf("unable to read specified client cert (%s): %w", c.CertFile, err) } } else { certData = []byte(c.Cert) @@ -986,7 +986,7 @@ func (c *TLSConfig) getClientCertificate(_ *tls.CertificateRequestInfo) (*tls.Ce if c.KeyFile != "" { keyData, err = os.ReadFile(c.KeyFile) if err != nil { - return nil, fmt.Errorf("unable to read specified client key (%s): %s", c.KeyFile, err) + return nil, fmt.Errorf("unable to read specified client key (%s): %w", c.KeyFile, err) } } else { keyData = []byte(c.Key) @@ -994,7 +994,7 @@ func (c *TLSConfig) getClientCertificate(_ *tls.CertificateRequestInfo) (*tls.Ce cert, err := tls.X509KeyPair(certData, keyData) if err != nil { - return nil, fmt.Errorf("unable to use specified client cert (%s) & key (%s): %s", c.CertFile, c.KeyFile, err) + return nil, fmt.Errorf("unable to use specified client cert (%s) & key (%s): %w", c.CertFile, c.KeyFile, err) } return &cert, nil @@ -1004,7 +1004,7 @@ func (c *TLSConfig) getClientCertificate(_ *tls.CertificateRequestInfo) (*tls.Ce func readCAFile(f string) ([]byte, error) { data, err := os.ReadFile(f) if err != nil { - return nil, fmt.Errorf("unable to load specified CA cert %s: %s", f, err) + return nil, fmt.Errorf("unable to load specified CA cert %s: %w", f, err) } return data, nil } diff --git a/expfmt/bench_test.go b/expfmt/bench_test.go index 3d7d68ec..dea89aee 100644 --- a/expfmt/bench_test.go +++ b/expfmt/bench_test.go @@ -16,6 +16,7 @@ package expfmt import ( "bytes" "compress/gzip" + "errors" "io" "os" "testing" @@ -101,7 +102,7 @@ func BenchmarkParseProto(b *testing.B) { for { family.Reset() if _, err := pbutil.ReadDelimited(in, family); err != nil { - if err == io.EOF { + if errors.Is(err, io.EOF) { break } b.Fatal(err) @@ -129,7 +130,7 @@ func BenchmarkParseProtoGzip(b *testing.B) { for { family.Reset() if _, err := pbutil.ReadDelimited(in, family); err != nil { - if err == io.EOF { + if errors.Is(err, io.EOF) { break } b.Fatal(err) @@ -156,7 +157,7 @@ func BenchmarkParseProtoMap(b *testing.B) { for { family := &dto.MetricFamily{} if _, err := pbutil.ReadDelimited(in, family); err != nil { - if err == io.EOF { + if errors.Is(err, io.EOF) { break } b.Fatal(err) diff --git a/expfmt/decode_test.go b/expfmt/decode_test.go index 56c8838b..7b7b41fd 100644 --- a/expfmt/decode_test.go +++ b/expfmt/decode_test.go @@ -15,6 +15,7 @@ package expfmt import ( "bufio" + "errors" "io" "net/http" "reflect" @@ -84,7 +85,7 @@ mf2 4 for { var smpls model.Vector err := dec.Decode(&smpls) - if err == io.EOF { + if err != nil && errors.Is(err, io.EOF) { break } if err != nil { @@ -346,7 +347,7 @@ func TestProtoDecoder(t *testing.T) { for { var smpls model.Vector err := dec.Decode(&smpls) - if err == io.EOF { + if err != nil && errors.Is(err, io.EOF) { break } if scenario.fail { @@ -505,7 +506,7 @@ func TestTextDecoderWithBufioReader(t *testing.T) { for { var mf dto.MetricFamily if err := dec.Decode(&mf); err != nil { - if err == io.EOF { + if errors.Is(err, io.EOF) { break } t.Fatalf("Unexpected error: %v", err) diff --git a/expfmt/text_parse.go b/expfmt/text_parse.go index 35db1cc9..196f5033 100644 --- a/expfmt/text_parse.go +++ b/expfmt/text_parse.go @@ -16,6 +16,7 @@ package expfmt import ( "bufio" "bytes" + "errors" "fmt" "io" "math" @@ -112,7 +113,7 @@ func (p *TextParser) TextToMetricFamilies(in io.Reader) (map[string]*dto.MetricF // stream. Turn this error into something nicer and more // meaningful. (io.EOF is often used as a signal for the legitimate end // of an input stream.) - if p.err == io.EOF { + if p.err != nil && errors.Is(p.err, io.EOF) { p.parseError("unexpected end of input stream") } return p.metricFamiliesByName, p.err @@ -146,7 +147,7 @@ func (p *TextParser) startOfLine() stateFn { // which is not an error but the signal that we are done. // Any other error that happens to align with the start of // a line is still an error. - if p.err == io.EOF { + if errors.Is(p.err, io.EOF) { p.err = nil } return nil diff --git a/expfmt/text_parse_test.go b/expfmt/text_parse_test.go index 893a8574..204a88a3 100644 --- a/expfmt/text_parse_test.go +++ b/expfmt/text_parse_test.go @@ -691,7 +691,7 @@ func TestTextParserStartOfLine(t *testing.T) { if fn != nil { t.Errorf("Unexpected non-nil function: %v", fn) } - if p.err != in.err { + if p.err != nil && !errors.Is(p.err, in.err) { t.Errorf("Unexpected error: %v, expected %v", p.err, in.err) } }) diff --git a/model/alert.go b/model/alert.go index 35e739c7..178fdbaf 100644 --- a/model/alert.go +++ b/model/alert.go @@ -90,13 +90,13 @@ func (a *Alert) Validate() error { return fmt.Errorf("start time must be before end time") } if err := a.Labels.Validate(); err != nil { - return fmt.Errorf("invalid label set: %s", err) + return fmt.Errorf("invalid label set: %w", err) } if len(a.Labels) == 0 { return fmt.Errorf("at least one label pair required") } if err := a.Annotations.Validate(); err != nil { - return fmt.Errorf("invalid annotations: %s", err) + return fmt.Errorf("invalid annotations: %w", err) } return nil } diff --git a/model/silence.go b/model/silence.go index bb99889d..910b0b71 100644 --- a/model/silence.go +++ b/model/silence.go @@ -81,7 +81,7 @@ func (s *Silence) Validate() error { } for _, m := range s.Matchers { if err := m.Validate(); err != nil { - return fmt.Errorf("invalid matcher: %s", err) + return fmt.Errorf("invalid matcher: %w", err) } } if s.StartsAt.IsZero() { diff --git a/model/value.go b/model/value.go index 9eb44041..71e0c50c 100644 --- a/model/value.go +++ b/model/value.go @@ -274,7 +274,7 @@ func (s *Scalar) UnmarshalJSON(b []byte) error { value, err := strconv.ParseFloat(f, 64) if err != nil { - return fmt.Errorf("error parsing sample value: %s", err) + return fmt.Errorf("error parsing sample value: %w", err) } s.Value = SampleValue(value) return nil