Skip to content

Commit

Permalink
errors.Wrap -> fmt.Errorf (#2317)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc authored Jun 29, 2023
1 parent e61d5a3 commit bd41f85
Show file tree
Hide file tree
Showing 15 changed files with 83 additions and 88 deletions.
16 changes: 7 additions & 9 deletions pkg/acquisition/modules/cloudwatch/cloudwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
"gopkg.in/tomb.v2"
Expand Down Expand Up @@ -201,7 +200,7 @@ func (cw *CloudwatchSource) Configure(yamlConfig []byte, logger *log.Entry) erro
targetStream := "*"
if cw.Config.StreamRegexp != nil {
if _, err := regexp.Compile(*cw.Config.StreamRegexp); err != nil {
return errors.Wrapf(err, "error while compiling regexp '%s'", *cw.Config.StreamRegexp)
return fmt.Errorf("while compiling regexp '%s': %w", *cw.Config.StreamRegexp, err)
}
targetStream = *cw.Config.StreamRegexp
} else if cw.Config.StreamName != nil {
Expand Down Expand Up @@ -345,8 +344,7 @@ func (cw *CloudwatchSource) WatchLogGroupForStreams(out chan LogStreamTailConfig
},
)
if err != nil {
newerr := errors.Wrapf(err, "while describing group %s", cw.Config.GroupName)
return newerr
return fmt.Errorf("while describing group %s: %w", cw.Config.GroupName, err)
}
cw.logger.Tracef("after DescribeLogStreamsPagesWithContext")
}
Expand Down Expand Up @@ -495,7 +493,7 @@ func (cw *CloudwatchSource) TailLogStream(cfg *LogStreamTailConfig, outChan chan
},
)
if err != nil {
newerr := errors.Wrapf(err, "while reading %s/%s", cfg.GroupName, cfg.StreamName)
newerr := fmt.Errorf("while reading %s/%s: %w", cfg.GroupName, cfg.StreamName, err)
cfg.logger.Warningf("err : %s", newerr)
return newerr
}
Expand Down Expand Up @@ -532,7 +530,7 @@ func (cw *CloudwatchSource) ConfigureByDSN(dsn string, labels map[string]string,

u, err := url.ParseQuery(args[1])
if err != nil {
return errors.Wrapf(err, "while parsing %s", dsn)
return fmt.Errorf("while parsing %s: %w", dsn, err)
}

for k, v := range u {
Expand All @@ -543,7 +541,7 @@ func (cw *CloudwatchSource) ConfigureByDSN(dsn string, labels map[string]string,
}
lvl, err := log.ParseLevel(v[0])
if err != nil {
return errors.Wrapf(err, "unknown level %s", v[0])
return fmt.Errorf("unknown level %s: %w", v[0], err)
}
cw.logger.Logger.SetLevel(lvl)

Expand Down Expand Up @@ -577,7 +575,7 @@ func (cw *CloudwatchSource) ConfigureByDSN(dsn string, labels map[string]string,
//let's reuse our parser helper so that a ton of date formats are supported
duration, err := time.ParseDuration(v[0])
if err != nil {
return errors.Wrapf(err, "unable to parse '%s' as duration", v[0])
return fmt.Errorf("unable to parse '%s' as duration: %w", v[0], err)
}
cw.logger.Debugf("parsed '%s' as '%s'", v[0], duration)
start := time.Now().UTC().Add(-duration)
Expand Down Expand Up @@ -674,7 +672,7 @@ func (cw *CloudwatchSource) CatLogStream(cfg *LogStreamTailConfig, outChan chan
},
)
if err != nil {
return errors.Wrapf(err, "while reading logs from %s/%s", cfg.GroupName, cfg.StreamName)
return fmt.Errorf("while reading logs from %s/%s: %w", cfg.GroupName, cfg.StreamName, err)
}
cfg.logger.Tracef("after GetLogEventsPagesWithContext")
case <-cw.t.Dying():
Expand Down
7 changes: 3 additions & 4 deletions pkg/acquisition/modules/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

dockerTypes "github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
"gopkg.in/tomb.v2"
Expand Down Expand Up @@ -80,7 +79,7 @@ func (d *DockerSource) UnmarshalConfig(yamlConfig []byte) error {

err := yaml.UnmarshalStrict(yamlConfig, &d.Config)
if err != nil {
return errors.Wrap(err, "Cannot parse DockerAcquisition configuration")
return fmt.Errorf("while parsing DockerAcquisition configuration: %w", err)
}

if d.logger != nil {
Expand Down Expand Up @@ -214,7 +213,7 @@ func (d *DockerSource) ConfigureByDSN(dsn string, labels map[string]string, logg

parameters, err := url.ParseQuery(args[1])
if err != nil {
return errors.Wrapf(err, "while parsing parameters %s: %s", dsn, err)
return fmt.Errorf("while parsing parameters %s: %w", dsn, err)
}

for k, v := range parameters {
Expand All @@ -225,7 +224,7 @@ func (d *DockerSource) ConfigureByDSN(dsn string, labels map[string]string, logg
}
lvl, err := log.ParseLevel(v[0])
if err != nil {
return errors.Wrapf(err, "unknown level %s", v[0])
return fmt.Errorf("unknown level %s: %w", v[0], err)
}
d.logger.Logger.SetLevel(lvl)
case "until":
Expand Down
22 changes: 11 additions & 11 deletions pkg/acquisition/modules/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import (
"strings"
"time"

"github.com/crowdsecurity/go-cs-lib/pkg/trace"

"github.com/fsnotify/fsnotify"
"github.com/nxadm/tail"
"github.com/pkg/errors"
Expand All @@ -24,6 +22,8 @@ import (
"gopkg.in/tomb.v2"
"gopkg.in/yaml.v2"

"github.com/crowdsecurity/go-cs-lib/pkg/trace"

"github.com/crowdsecurity/crowdsec/pkg/acquisition/configuration"
"github.com/crowdsecurity/crowdsec/pkg/types"
)
Expand Down Expand Up @@ -110,7 +110,7 @@ func (f *FileSource) Configure(yamlConfig []byte, logger *log.Entry) error {

f.watcher, err = fsnotify.NewWatcher()
if err != nil {
return errors.Wrapf(err, "Could not create fsnotify watcher")
return fmt.Errorf("could not create fsnotify watcher: %w", err)
}

f.logger.Tracef("Actual FileAcquisition Configuration %+v", f.config)
Expand All @@ -130,7 +130,7 @@ func (f *FileSource) Configure(yamlConfig []byte, logger *log.Entry) error {
}
files, err := filepath.Glob(pattern)
if err != nil {
return errors.Wrap(err, "Glob failure")
return fmt.Errorf("glob failure: %w", err)
}
if len(files) == 0 {
f.logger.Warnf("No matching files for pattern %s", pattern)
Expand Down Expand Up @@ -191,7 +191,7 @@ func (f *FileSource) ConfigureByDSN(dsn string, labels map[string]string, logger
if len(args) == 2 && len(args[1]) != 0 {
params, err := url.ParseQuery(args[1])
if err != nil {
return errors.Wrap(err, "could not parse file args")
return fmt.Errorf("could not parse file args: %w", err)
}
for key, value := range params {
switch key {
Expand All @@ -201,7 +201,7 @@ func (f *FileSource) ConfigureByDSN(dsn string, labels map[string]string, logger
}
lvl, err := log.ParseLevel(value[0])
if err != nil {
return errors.Wrapf(err, "unknown level %s", value[0])
return fmt.Errorf("unknown level %s: %w", value[0], err)
}
f.logger.Logger.SetLevel(lvl)
case "max_buffer_size":
Expand All @@ -210,7 +210,7 @@ func (f *FileSource) ConfigureByDSN(dsn string, labels map[string]string, logger
}
maxBufferSize, err := strconv.Atoi(value[0])
if err != nil {
return errors.Wrapf(err, "could not parse max_buffer_size %s", value[0])
return fmt.Errorf("could not parse max_buffer_size %s: %w", value[0], err)
}
f.config.MaxBufferSize = maxBufferSize
default:
Expand All @@ -226,7 +226,7 @@ func (f *FileSource) ConfigureByDSN(dsn string, labels map[string]string, logger
f.logger.Debugf("Will try pattern %s", args[0])
files, err := filepath.Glob(args[0])
if err != nil {
return errors.Wrap(err, "Glob failure")
return fmt.Errorf("glob failure: %w", err)
}

if len(files) == 0 {
Expand Down Expand Up @@ -433,7 +433,7 @@ func (f *FileSource) monitorNewFiles(out chan types.Event, t *tomb.Tomb) error {
case <-t.Dying():
err := f.watcher.Close()
if err != nil {
return errors.Wrapf(err, "could not remove all inotify watches")
return fmt.Errorf("could not remove all inotify watches: %w", err)
}
return nil
}
Expand Down Expand Up @@ -495,15 +495,15 @@ func (f *FileSource) readFile(filename string, out chan types.Event, t *tomb.Tom
fd, err := os.Open(filename)

if err != nil {
return errors.Wrapf(err, "failed opening %s", filename)
return fmt.Errorf("failed opening %s: %w", filename, err)
}
defer fd.Close()

if strings.HasSuffix(filename, ".gz") {
gz, err := gzip.NewReader(fd)
if err != nil {
logger.Errorf("Failed to read gz file: %s", err)
return errors.Wrapf(err, "failed to read gz %s", filename)
return fmt.Errorf("failed to read gz %s: %w", filename, err)
}
defer gz.Close()
scanner = bufio.NewScanner(gz)
Expand Down
4 changes: 2 additions & 2 deletions pkg/acquisition/modules/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestBadConfiguration(t *testing.T) {
{
name: "glob syntax error",
config: `filename: "[asd-.log"`,
expectedErr: "Glob failure: syntax error in pattern",
expectedErr: "glob failure: syntax error in pattern",
},
{
name: "bad exclude regexp",
Expand Down Expand Up @@ -150,7 +150,7 @@ filename: /`,
config: `
mode: cat
filename: "[*-.log"`,
expectedConfigErr: "Glob failure: syntax error in pattern",
expectedConfigErr: "glob failure: syntax error in pattern",
logLevel: log.WarnLevel,
expectedLines: 0,
},
Expand Down
3 changes: 1 addition & 2 deletions pkg/acquisition/modules/journalctl/journalctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"strings"
"time"

"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
"gopkg.in/tomb.v2"
Expand Down Expand Up @@ -237,7 +236,7 @@ func (j *JournalCtlSource) ConfigureByDSN(dsn string, labels map[string]string,
}
lvl, err := log.ParseLevel(value[0])
if err != nil {
return errors.Wrapf(err, "unknown level %s", value[0])
return fmt.Errorf("unknown level %s: %w", value[0], err)
}
j.logger.Logger.SetLevel(lvl)
case "since":
Expand Down
11 changes: 5 additions & 6 deletions pkg/acquisition/modules/kafka/kafka.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"strconv"
"time"

"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/segmentio/kafka-go"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -93,12 +92,12 @@ func (k *KafkaSource) Configure(yamlConfig []byte, logger *log.Entry) error {

dialer, err := k.Config.NewDialer()
if err != nil {
return errors.Wrapf(err, "cannot create %s dialer", dataSourceName)
return fmt.Errorf("cannot create %s dialer: %w", dataSourceName, err)
}

k.Reader, err = k.Config.NewReader(dialer)
if err != nil {
return errors.Wrapf(err, "cannote create %s reader", dataSourceName)
return fmt.Errorf("cannote create %s reader: %w", dataSourceName, err)
}

if k.Reader == nil {
Expand Down Expand Up @@ -149,7 +148,7 @@ func (k *KafkaSource) ReadMessage(out chan types.Event) error {
if err == io.EOF {
return nil
}
k.logger.Errorln(errors.Wrapf(err, "while reading %s message", dataSourceName))
k.logger.Errorln(fmt.Errorf("while reading %s message: %w", dataSourceName, err))
}
l := types.Line{
Raw: string(m.Value),
Expand Down Expand Up @@ -181,7 +180,7 @@ func (k *KafkaSource) RunReader(out chan types.Event, t *tomb.Tomb) error {
case <-t.Dying():
k.logger.Infof("%s datasource topic %s stopping", dataSourceName, k.Config.Topic)
if err := k.Reader.Close(); err != nil {
return errors.Wrapf(err, "while closing %s reader on topic '%s'", dataSourceName, k.Config.Topic)
return fmt.Errorf("while closing %s reader on topic '%s': %w", dataSourceName, k.Config.Topic, err)
}
return nil
}
Expand Down Expand Up @@ -264,7 +263,7 @@ func (kc *KafkaConfiguration) NewReader(dialer *kafka.Dialer) (*kafka.Reader, er
rConf.GroupID = kc.GroupID
}
if err := rConf.Validate(); err != nil {
return &kafka.Reader{}, errors.Wrapf(err, "while validating reader configuration")
return &kafka.Reader{}, fmt.Errorf("while validating reader configuration: %w", err)
}
return kafka.NewReader(rConf), nil
}
14 changes: 7 additions & 7 deletions pkg/acquisition/modules/kubernetesaudit/k8s_audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ import (
"net/http"
"strings"

"github.com/crowdsecurity/go-cs-lib/pkg/trace"

"github.com/crowdsecurity/crowdsec/pkg/acquisition/configuration"
"github.com/crowdsecurity/crowdsec/pkg/types"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
"gopkg.in/tomb.v2"
"gopkg.in/yaml.v2"
"k8s.io/apiserver/pkg/apis/audit"

"github.com/crowdsecurity/go-cs-lib/pkg/trace"

"github.com/crowdsecurity/crowdsec/pkg/acquisition/configuration"
"github.com/crowdsecurity/crowdsec/pkg/types"
)

type KubernetesAuditConfiguration struct {
Expand Down Expand Up @@ -66,7 +66,7 @@ func (ka *KubernetesAuditSource) UnmarshalConfig(yamlConfig []byte) error {
k8sConfig := KubernetesAuditConfiguration{}
err := yaml.UnmarshalStrict(yamlConfig, &k8sConfig)
if err != nil {
return errors.Wrap(err, "Cannot parse k8s-audit configuration")
return fmt.Errorf("cannot parse k8s-audit configuration: %w", err)
}

ka.config = k8sConfig
Expand Down Expand Up @@ -140,7 +140,7 @@ func (ka *KubernetesAuditSource) StreamingAcquisition(out chan types.Event, t *t
t.Go(func() error {
err := ka.server.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
return errors.Wrap(err, "k8s-audit server failed")
return fmt.Errorf("k8s-audit server failed: %w", err)
}
return nil
})
Expand Down
13 changes: 7 additions & 6 deletions pkg/cwhub/cwhub.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func GetItemByPath(itemType string, itemPath string) (*Item, error) {
finalName := ""
f, err := os.Lstat(itemPath)
if err != nil {
return nil, errors.Wrapf(err, "while performing lstat on %s", itemPath)
return nil, fmt.Errorf("while performing lstat on %s: %w", itemPath, err)
}

if f.Mode()&os.ModeSymlink == 0 {
Expand All @@ -156,7 +156,7 @@ func GetItemByPath(itemType string, itemPath string) (*Item, error) {
/*resolve the symlink to hub file*/
pathInHub, err := os.Readlink(itemPath)
if err != nil {
return nil, errors.Wrapf(err, "while reading symlink of %s", itemPath)
return nil, fmt.Errorf("while reading symlink of %s: %w", itemPath, err)
}
//extract author from path
fname := filepath.Base(pathInHub)
Expand Down Expand Up @@ -238,7 +238,7 @@ func GetInstalledScenariosAsString() ([]string, error) {

items, err := GetInstalledScenarios()
if err != nil {
return nil, errors.Wrap(err, "while fetching scenarios")
return nil, fmt.Errorf("while fetching scenarios: %w", err)
}
for _, it := range items {
retStr = append(retStr, it.Name)
Expand Down Expand Up @@ -279,7 +279,7 @@ func GetInstalledParsersAsString() ([]string, error) {

items, err := GetInstalledParsers()
if err != nil {
return nil, errors.Wrap(err, "while fetching parsers")
return nil, fmt.Errorf("while fetching parsers: %w", err)
}
for _, it := range items {
retStr = append(retStr, it.Name)
Expand All @@ -306,7 +306,7 @@ func GetInstalledPostOverflowsAsString() ([]string, error) {

items, err := GetInstalledPostOverflows()
if err != nil {
return nil, errors.Wrap(err, "while fetching post overflows")
return nil, fmt.Errorf("while fetching post overflows: %w", err)
}
for _, it := range items {
retStr = append(retStr, it.Name)
Expand All @@ -319,8 +319,9 @@ func GetInstalledCollectionsAsString() ([]string, error) {

items, err := GetInstalledCollections()
if err != nil {
return nil, errors.Wrap(err, "while fetching collections")
return nil, fmt.Errorf("while fetching collections: %w", err)
}

for _, it := range items {
retStr = append(retStr, it.Name)
}
Expand Down
Loading

0 comments on commit bd41f85

Please sign in to comment.