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

chore(linters): replacing 'fmt.Errorf' with 'errors.New' wherever possible #14698

Merged
merged 2 commits into from
Feb 8, 2024
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
8 changes: 4 additions & 4 deletions agent/accumulator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package agent

import (
"bytes"
"fmt"
"errors"
"log"
"os"
"testing"
Expand Down Expand Up @@ -55,9 +55,9 @@ func TestAccAddError(t *testing.T) {
defer close(metrics)
a := NewAccumulator(&TestMetricMaker{}, metrics)

a.AddError(fmt.Errorf("foo"))
a.AddError(fmt.Errorf("bar"))
a.AddError(fmt.Errorf("baz"))
a.AddError(errors.New("foo"))
a.AddError(errors.New("bar"))
a.AddError(errors.New("baz"))

errs := bytes.Split(errBuf.Bytes(), []byte{'\n'})
require.Len(t, errs, 4) // 4 because of trailing newline
Expand Down
10 changes: 5 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ func (c *Config) LoadConfigData(data []byte) error {
if val, ok := tbl.Fields["agent"]; ok {
subTable, ok := val.(*ast.Table)
if !ok {
return fmt.Errorf("invalid configuration, error parsing agent table")
return errors.New("invalid configuration, error parsing agent table")
}
if err = c.toml.UnmarshalTable(subTable, c.Agent); err != nil {
return fmt.Errorf("error parsing [agent]: %w", err)
Expand Down Expand Up @@ -794,7 +794,7 @@ func (c *Config) addAggregator(name string, table *ast.Table) error {
// Handle removed, deprecated plugins
if di, deprecated := aggregators.Deprecations[name]; deprecated {
printHistoricPluginDeprecationNotice("aggregators", name, di)
return fmt.Errorf("plugin deprecated")
return errors.New("plugin deprecated")
}
return fmt.Errorf("undefined but requested aggregator: %s", name)
}
Expand Down Expand Up @@ -980,7 +980,7 @@ func (c *Config) addProcessor(name string, table *ast.Table) error {
// Handle removed, deprecated plugins
if di, deprecated := processors.Deprecations[name]; deprecated {
printHistoricPluginDeprecationNotice("processors", name, di)
return fmt.Errorf("plugin deprecated")
return errors.New("plugin deprecated")
}
return fmt.Errorf("undefined but requested processor: %s", name)
}
Expand Down Expand Up @@ -1109,7 +1109,7 @@ func (c *Config) addOutput(name string, table *ast.Table) error {
// Handle removed, deprecated plugins
if di, deprecated := outputs.Deprecations[name]; deprecated {
printHistoricPluginDeprecationNotice("outputs", name, di)
return fmt.Errorf("plugin deprecated")
return errors.New("plugin deprecated")
}
return fmt.Errorf("undefined but requested output: %s", name)
}
Expand Down Expand Up @@ -1191,7 +1191,7 @@ func (c *Config) addInput(name string, table *ast.Table) error {
// Handle removed, deprecated plugins
if di, deprecated := inputs.Deprecations[name]; deprecated {
printHistoricPluginDeprecationNotice("inputs", name, di)
return fmt.Errorf("plugin deprecated")
return errors.New("plugin deprecated")
}

return fmt.Errorf("undefined but requested input: %s", name)
Expand Down
3 changes: 2 additions & 1 deletion config/deprecation.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"errors"
"fmt"
"log"
"reflect"
Expand Down Expand Up @@ -171,7 +172,7 @@ func (c *Config) printUserDeprecation(category, name string, plugin interface{})
models.PrintPluginDeprecationNotice(info.LogLevel, info.Name, info.info)

if info.LogLevel == telegraf.Error {
return fmt.Errorf("plugin deprecated")
return errors.New("plugin deprecated")
}

// Print deprecated options
Expand Down
3 changes: 2 additions & 1 deletion internal/snmp/translate.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package snmp

import (
"errors"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -271,7 +272,7 @@ func SnmpTranslateCall(oid string) (mibName string, oidNum string, oidText strin
oidText = out.RenderQualified()
i := strings.Index(oidText, "::")
if i == -1 {
return "", oid, oid, oid, out, fmt.Errorf("not found")
return "", oid, oid, oid, out, errors.New("not found")
}
mibName = oidText[:i]
oidText = oidText[i+2:] + end
Expand Down
9 changes: 5 additions & 4 deletions internal/snmp/wrapper.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package snmp

import (
"errors"
"fmt"
"net/url"
"strconv"
Expand Down Expand Up @@ -46,7 +47,7 @@ func NewWrapper(s ClientConfig) (GosnmpWrapper, error) {
case 1:
gs.Version = gosnmp.Version1
default:
return GosnmpWrapper{}, fmt.Errorf("invalid version")
return GosnmpWrapper{}, errors.New("invalid version")
}

if s.Version < 3 {
Expand Down Expand Up @@ -74,7 +75,7 @@ func NewWrapper(s ClientConfig) (GosnmpWrapper, error) {
case "authpriv":
gs.MsgFlags = gosnmp.AuthPriv
default:
return GosnmpWrapper{}, fmt.Errorf("invalid secLevel")
return GosnmpWrapper{}, errors.New("invalid secLevel")
}

sp.UserName = s.SecName
Expand All @@ -95,7 +96,7 @@ func NewWrapper(s ClientConfig) (GosnmpWrapper, error) {
case "":
sp.AuthenticationProtocol = gosnmp.NoAuth
default:
return GosnmpWrapper{}, fmt.Errorf("invalid authProtocol")
return GosnmpWrapper{}, errors.New("invalid authProtocol")
}

sp.AuthenticationPassphrase = s.AuthPassword
Expand All @@ -116,7 +117,7 @@ func NewWrapper(s ClientConfig) (GosnmpWrapper, error) {
case "":
sp.PrivacyProtocol = gosnmp.NoPriv
default:
return GosnmpWrapper{}, fmt.Errorf("invalid privProtocol")
return GosnmpWrapper{}, errors.New("invalid privProtocol")
}

sp.PrivacyPassphrase = s.PrivPassword
Expand Down
6 changes: 3 additions & 3 deletions internal/syslog/framing.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package syslog

import (
"fmt"
"errors"
"strings"
)

Expand Down Expand Up @@ -50,7 +50,7 @@ func (f *Framing) UnmarshalText(data []byte) error {
return nil
}
*f = -1
return fmt.Errorf("unknown framing")
return errors.New("unknown framing")
}

// MarshalText implements encoding.TextMarshaller
Expand All @@ -59,5 +59,5 @@ func (f Framing) MarshalText() ([]byte, error) {
if s != "" {
return []byte(s), nil
}
return nil, fmt.Errorf("unknown framing")
return nil, errors.New("unknown framing")
}
6 changes: 3 additions & 3 deletions models/running_output_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package models

import (
"fmt"
"errors"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -532,7 +532,7 @@ func (m *mockOutput) Write(metrics []telegraf.Metric) error {
m.Lock()
defer m.Unlock()
if m.failWrite {
return fmt.Errorf("failed write")
return errors.New("failed write")
}

if m.metrics == nil {
Expand Down Expand Up @@ -572,7 +572,7 @@ func (m *perfOutput) SampleConfig() string {

func (m *perfOutput) Write(_ []telegraf.Metric) error {
if m.failWrite {
return fmt.Errorf("failed write")
return errors.New("failed write")
}
return nil
}
9 changes: 5 additions & 4 deletions plugins/common/cookie/cookie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cookie

import (
"context"
"fmt"
"errors"
"io"
"net/http"
"net/http/httptest"
Expand All @@ -12,9 +12,10 @@ import (

clockutil "github.com/benbjohnson/clock"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/require"

"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/require"
)

const (
Expand Down Expand Up @@ -189,7 +190,7 @@ func TestAuthConfig_Start(t *testing.T) {
renewal: renewal,
endpoint: authEndpointWithBasicAuth,
},
wantErr: fmt.Errorf("cookie auth renewal received status code: 401 (Unauthorized) []"),
wantErr: errors.New("cookie auth renewal received status code: 401 (Unauthorized) []"),
firstAuthCount: 0,
lastAuthCount: 0,
firstHTTPResponse: http.StatusForbidden,
Expand Down Expand Up @@ -220,7 +221,7 @@ func TestAuthConfig_Start(t *testing.T) {
renewal: renewal,
endpoint: authEndpointWithBody,
},
wantErr: fmt.Errorf("cookie auth renewal received status code: 401 (Unauthorized) []"),
wantErr: errors.New("cookie auth renewal received status code: 401 (Unauthorized) []"),
firstAuthCount: 0,
lastAuthCount: 0,
firstHTTPResponse: http.StatusForbidden,
Expand Down
5 changes: 3 additions & 2 deletions plugins/common/kafka/config.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package kafka

import (
"fmt"
"errors"
"math"
"strings"
"time"

"github.com/IBM/sarama"

"github.com/influxdata/telegraf"
tgConf "github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/plugins/common/tls"
Expand Down Expand Up @@ -141,7 +142,7 @@ func (k *Config) SetConfig(config *sarama.Config, log telegraf.Logger) error {

switch strings.ToLower(k.MetadataRetryType) {
default:
return fmt.Errorf("invalid metadata retry type")
return errors.New("invalid metadata retry type")
case "exponential":
if k.MetadataRetryBackoff == 0 {
k.MetadataRetryBackoff = tgConf.Duration(250 * time.Millisecond)
Expand Down
7 changes: 4 additions & 3 deletions plugins/common/opcua/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package opcua

import (
"context"
"errors"
"fmt"
"log" //nolint:depguard // just for debug
"net/url"
Expand Down Expand Up @@ -67,12 +68,12 @@ func (o *OpcUAClientConfig) validateOptionalFields() error {

func (o *OpcUAClientConfig) validateEndpoint() error {
if o.Endpoint == "" {
return fmt.Errorf("endpoint url is empty")
return errors.New("endpoint url is empty")
}

_, err := url.Parse(o.Endpoint)
if err != nil {
return fmt.Errorf("endpoint url is invalid")
return errors.New("endpoint url is invalid")
}

switch o.SecurityPolicy {
Expand Down Expand Up @@ -224,7 +225,7 @@ func (o *OpcUAClient) Disconnect(ctx context.Context) error {
o.Client = nil
return err
default:
return fmt.Errorf("invalid controller")
return errors.New("invalid controller")
}
}

Expand Down
6 changes: 3 additions & 3 deletions plugins/common/opcua/input/input_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ type InputClientConfig struct {

func (o *InputClientConfig) Validate() error {
if o.MetricName == "" {
return fmt.Errorf("metric name is empty")
return errors.New("metric name is empty")
}

err := choice.Check(string(o.Timestamp), []string{"", "gather", "server", "source"})
Expand Down Expand Up @@ -278,11 +278,11 @@ func validateNodeToAdd(existing map[metricParts]struct{}, nmm *NodeMetricMapping
}

if len(nmm.Tag.Namespace) == 0 {
return fmt.Errorf("empty node namespace not allowed")
return errors.New("empty node namespace not allowed")
}

if len(nmm.Tag.Identifier) == 0 {
return fmt.Errorf("empty node identifier not allowed")
return errors.New("empty node identifier not allowed")
}

mp := newMP(nmm)
Expand Down
11 changes: 5 additions & 6 deletions plugins/common/opcua/input/input_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package input

import (
"errors"
"fmt"
"testing"
"time"

Expand Down Expand Up @@ -39,9 +38,9 @@ func TestTagsSliceToMap_dupeKey(t *testing.T) {

func TestTagsSliceToMap_empty(t *testing.T) {
_, err := tagsSliceToMap([][]string{{"foo", ""}})
require.Equal(t, fmt.Errorf("tag 1 has empty value"), err)
require.Equal(t, errors.New("tag 1 has empty value"), err)
_, err = tagsSliceToMap([][]string{{"", "bar"}})
require.Equal(t, fmt.Errorf("tag 1 has empty name"), err)
require.Equal(t, errors.New("tag 1 has empty name"), err)
}

func TestValidateOPCTags(t *testing.T) {
Expand Down Expand Up @@ -91,7 +90,7 @@ func TestValidateOPCTags(t *testing.T) {
},
},
},
fmt.Errorf("tag 1 has empty value"),
errors.New("tag 1 has empty value"),
},
{
"empty tag name not allowed",
Expand All @@ -105,7 +104,7 @@ func TestValidateOPCTags(t *testing.T) {
},
},
},
fmt.Errorf("tag 1 has empty name"),
errors.New("tag 1 has empty name"),
},
{
"different metric tag names",
Expand Down Expand Up @@ -370,7 +369,7 @@ func TestValidateNodeToAdd(t *testing.T) {
}, map[string]string{})
return nmm
}(),
err: fmt.Errorf("empty node namespace not allowed"),
err: errors.New("empty node namespace not allowed"),
},
{
name: "empty identifier type not allowed",
Expand Down
7 changes: 4 additions & 3 deletions plugins/common/opcua/opcua_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"errors"
"fmt"
"math/big"
"net"
Expand All @@ -34,7 +35,7 @@ func generateCert(host string, rsaBits int, certFile, keyFile string, dur time.D
dir, _ := newTempDir()

if len(host) == 0 {
return "", "", fmt.Errorf("missing required host parameter")
return "", "", errors.New("missing required host parameter")
}
if rsaBits == 0 {
rsaBits = 2048
Expand Down Expand Up @@ -181,7 +182,7 @@ func (o *OpcUAClient) generateClientOpts(endpoints []*ua.EndpointDescription) ([
} else {
pk, ok := c.PrivateKey.(*rsa.PrivateKey)
if !ok {
return nil, fmt.Errorf("invalid private key")
return nil, errors.New("invalid private key")
}
cert = c.Certificate[0]
opts = append(opts, opcua.PrivateKey(pk), opcua.Certificate(cert))
Expand Down Expand Up @@ -276,7 +277,7 @@ func (o *OpcUAClient) generateClientOpts(endpoints []*ua.EndpointDescription) ([
}

if serverEndpoint == nil { // Didn't find an endpoint with matching policy and mode.
return nil, fmt.Errorf("unable to find suitable server endpoint with selected sec-policy and sec-mode")
return nil, errors.New("unable to find suitable server endpoint with selected sec-policy and sec-mode")
}

secPolicy = serverEndpoint.SecurityPolicyURI
Expand Down
Loading
Loading