Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable errorlint linter #550

Merged
merged 1 commit into from
Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
linters:
enable:
- errorlint
- misspell
- revive

Expand Down
16 changes: 8 additions & 8 deletions config/http_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -986,15 +986,15 @@ 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)
}

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
Expand All @@ -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
}
Expand Down
7 changes: 4 additions & 3 deletions expfmt/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package expfmt
import (
"bytes"
"compress/gzip"
"errors"
"io"
"os"
"testing"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions expfmt/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package expfmt

import (
"bufio"
"errors"
"io"
"net/http"
"reflect"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions expfmt/text_parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package expfmt
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"math"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion expfmt/text_parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
Expand Down
4 changes: 2 additions & 2 deletions model/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion model/silence.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion model/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down