Skip to content

Commit

Permalink
supported go1.13 error wrap
Browse files Browse the repository at this point in the history
  • Loading branch information
mimuret committed Feb 11, 2021
1 parent bde25f5 commit afa02ac
Show file tree
Hide file tree
Showing 16 changed files with 50 additions and 188 deletions.
6 changes: 3 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,10 @@ func NewConfigFromReader(r io.Reader) (*Config, error) {
v.SetConfigType("toml")
v.SetDefault("InputMsgBuffer", 10000)
if err := v.ReadConfig(r); err != nil {
return nil, errors.Wrap(err, "can't read config")
return nil, fmt.Errorf("failed to read config: %w", err)
}
if err := v.Unmarshal(c); err != nil {
return nil, errors.Wrap(err, "can't parse config")
return nil, fmt.Errorf("failed to parse config: %w", err)
}
return c, nil
}
Expand Down Expand Up @@ -592,7 +592,7 @@ func (o *OutputStdoutConfig) Validate() error {
}
t, err := template.New("stdout").Parse(o.TemplateStr)
if err != nil {
valerr.Add(errors.Wrap(err, "Template parse error"))
valerr.Add(fmt.Errorf("Template parse error: %w", err))
}
o.template = t
default:
Expand Down
7 changes: 4 additions & 3 deletions dnstap_fluentd_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
package dtap

import (
"fmt"

dnstap "github.com/dnstap/golang-dnstap"
framestream "github.com/farsightsec/golang-framestream"
"github.com/pkg/errors"

"github.com/fluent/fluent-logger-golang/fluent"
"github.com/golang/protobuf/proto"
Expand Down Expand Up @@ -52,7 +53,7 @@ func (o *DnstapFluentdOutput) open() error {
var err error
o.client, err = fluent.New(o.fluetConfig)
if err != nil {
return errors.Wrapf(err, "can't create fluent logger")
return fmt.Errorf("failed to create fluent logger: %w", err)
}

return nil
Expand All @@ -68,7 +69,7 @@ func (o *DnstapFluentdOutput) write(frame []byte) error {
return err
}
if err := o.client.Post(o.tag, *data); err != nil {
return errors.Wrapf(err, "failed to post fluent message, tag: %s", o.tag)
return fmt.Errorf("failed to post fluent message, tag: %s %w", o.tag, err)
}
return nil
}
Expand Down
10 changes: 5 additions & 5 deletions dnstap_fstrm_file_input.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import (
"compress/bzip2"
"compress/gzip"
"context"
"fmt"
"io"
"os"
"strings"

"github.com/pkg/errors"
"github.com/ulikunitz/xz"
)

Expand Down Expand Up @@ -55,13 +55,13 @@ func NewDnstapFstrmFileInput(config *InputFileConfig) (*DnstapFstrmFileInput, er
var r io.ReadCloser
f, err := os.Open(config.GetPath())
if err != nil {
return nil, errors.Wrapf(err, "watch failed, path: %s", config.GetPath())
return nil, fmt.Errorf("failed to watci file, path: %s err: %w", config.GetPath(), err)
}

if strings.HasSuffix(config.GetPath(), "gz") {
r, err = gzip.NewReader(f)
if err != nil {
return nil, errors.Wrapf(err, "failed to create gzip reader, path: %s", config.GetPath())
return nil, fmt.Errorf("failed to create gzip reader, path: %s err: %w", config.GetPath(), err)
}
} else if strings.HasSuffix(config.GetPath(), "bz2") {
cmp := bzip2.NewReader(f)
Expand All @@ -70,14 +70,14 @@ func NewDnstapFstrmFileInput(config *InputFileConfig) (*DnstapFstrmFileInput, er
cmp, err := xz.NewReader(f)
r = NewDnstapFstrmFileReadCloser(cmp, f)
if err != nil {
return nil, errors.Wrapf(err, "failed to create xz reader, path: %s", config.GetPath())
return nil, fmt.Errorf("failed to create xz reader, path: %s err: %w", config.GetPath(), err)
}
} else {
r = f
}
input, err := NewDnstapFstrmInput(r, false)
if err != nil {
return nil, errors.Wrapf(err, "failed to create fstrm input, path: %s", config.GetPath())
return nil, fmt.Errorf("failed to create fstrm input, path: %s err: %w", config.GetPath(), err)
}

i := &DnstapFstrmFileInput{
Expand Down
6 changes: 3 additions & 3 deletions dnstap_fstrm_file_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
package dtap

import (
"fmt"
"io"
"os"
"time"

dnstap "github.com/dnstap/golang-dnstap"
framestream "github.com/farsightsec/golang-framestream"
strftime "github.com/jehiah/go-strftime"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)

Expand All @@ -49,13 +49,13 @@ func (o *DnstapFstrmFileOutput) open() error {

f, err := os.OpenFile(filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
if err != nil {
return errors.Wrapf(err, "can't create file %s", filename)
return fmt.Errorf("failed to create file %s err: %w", filename, err)
}
o.writer = f

o.enc, err = framestream.NewEncoder(o.writer, &framestream.EncoderOptions{ContentType: dnstap.FSContentType, Bidirectional: false})
if err != nil {
return errors.Wrapf(err, "can't create framestream encorder %s", filename)
return fmt.Errorf("failed to create framestream encorder %s err: %w", filename, err)
}
o.currentFilename = filename
o.opened = make(chan bool)
Expand Down
7 changes: 3 additions & 4 deletions dnstap_fstrm_input.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ package dtap

import (
"context"
"fmt"
"io"

"github.com/pkg/errors"

dnstap "github.com/dnstap/golang-dnstap"

framestream "github.com/farsightsec/golang-framestream"
Expand All @@ -40,7 +39,7 @@ func NewDnstapFstrmInput(rc io.ReadCloser, bi bool) (*DnstapFstrmInput, error) {
Bidirectional: bi,
})
if err != nil {
return nil, errors.Wrapf(err, "can't create framestream Decoder")
return nil, fmt.Errorf("failed to create framestream Decoder: %w", err)
}
return &DnstapFstrmInput{
rc: rc,
Expand All @@ -56,7 +55,7 @@ func (i *DnstapFstrmInput) read(rbuf *RBuf) {
i.readError <- nil
return
}
i.readError <- errors.Wrap(err, "decode error")
i.readError <- fmt.Errorf("decode error: %w", err)
return
}
newbuf := make([]byte, len(buf))
Expand Down
6 changes: 3 additions & 3 deletions dnstap_fstrm_socket_input.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ package dtap

import (
"context"
"fmt"
"net"
"strings"

"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -49,12 +49,12 @@ func (i *DnstapFstrmSocketInput) runRead(ctx context.Context, rbuf *RBuf) {
i.readError <- nil
return
}
i.readError <- errors.Wrapf(err, "can't accept socket")
i.readError <- fmt.Errorf("failed to accept socket", err)
return
}
input, err := NewDnstapFstrmInput(conn, true)
if err != nil {
log.Debugf("can't create NewDnstapFstrmInput: %s", err)
log.Debugf("failed to create NewDnstapFstrmInput: %s", err)
continue
}
childCtx, _ := context.WithCancel(readCtx)
Expand Down
4 changes: 2 additions & 2 deletions dnstap_fstrm_socket_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
package dtap

import (
"fmt"
"time"

framestream "github.com/farsightsec/golang-framestream"
"github.com/pkg/errors"
)

type DnstapFstrmSocketOutput struct {
Expand All @@ -39,7 +39,7 @@ func NewDnstapFstrmSocketOutput(handler SocketOutput, params *DnstapOutputParams
func (o *DnstapFstrmSocketOutput) open() error {
var err error
if o.enc, err = o.handler.newConnect(); err != nil {
return errors.Wrapf(err, "can't connect socket")
return fmt.Errorf("failed to connect socket: %w")
}
o.opened = make(chan bool)
go func() {
Expand Down
8 changes: 4 additions & 4 deletions dnstap_fstrm_tail_input.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ package dtap

import (
"context"
"fmt"
"os"
"path/filepath"
"time"

"github.com/pkg/errors"
log "github.com/sirupsen/logrus"

"github.com/fsnotify/fsnotify"
Expand Down Expand Up @@ -50,7 +50,7 @@ func (i *DnstapFstrmTailInput) runSearchPath(ctx context.Context) error {
for {
matches, err := filepath.Glob(i.config.GetPath())
if err != nil {
return errors.Wrapf(err, "search file error, path: %s", i.config.GetPath())
return fmt.Errorf("search file error, path: %s err: %w", i.config.GetPath(), err)
} else {
for _, filename := range matches {
if _, ok := i.readers[filename]; ok != false {
Expand All @@ -76,11 +76,11 @@ func (i *DnstapFstrmTailInput) runReadFile(ctx context.Context, filename string,
modify := make(chan bool)
f, err := os.Open(filename)
if err != nil {
return errors.Wrapf(err, "can't open file, path: %s", filename)
return fmt.Errorf("failed to open file, path: %s err: %w", filename, err)
}
watcher, err := fsnotify.NewWatcher()
if err != nil {
return errors.Wrapf(err, "can't create file watcher")
return fmt.Errorf("failed to create file watcher: %w", err)
}
defer watcher.Close()
watcher.Add(filename)
Expand Down
5 changes: 2 additions & 3 deletions dnstap_fstrm_tcp_socket_input.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@
package dtap

import (
"fmt"
"net"

"github.com/pkg/errors"
)

func NewDnstapFstrmTCPSocketInput(config *InputTCPSocketConfig) (*DnstapFstrmSocketInput, error) {
l, err := net.Listen("tcp", config.GetNet())
if err != nil {
return nil, errors.Wrapf(err, "can't listen %s", config.GetNet())
return nil, fmt.Errorf("failed to listen %s err: %w", config.GetNet(), err)
}
return NewDnstapFstrmSocketInput(l)
}
7 changes: 3 additions & 4 deletions dnstap_fstrm_tcp_socket_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
package dtap

import (
"fmt"
"net"

dnstap "github.com/dnstap/golang-dnstap"
framestream "github.com/farsightsec/golang-framestream"
"github.com/pkg/errors"
)

type DnstapFstrmTCPSocketOutput struct {
Expand All @@ -37,12 +37,11 @@ func (o *DnstapFstrmTCPSocketOutput) newConnect() (*framestream.Encoder, error)
w, err := net.Dial("tcp", o.config.GetAddress())
if err != nil {

return nil, errors.Wrapf(err, "can't connect tcp socket, address: %s", o.config.GetAddress())
return nil, fmt.Errorf("failed to connect tcp socket, address: %s err: %w", o.config.GetAddress(), err)
}
enc, err := framestream.NewEncoder(w, &framestream.EncoderOptions{ContentType: dnstap.FSContentType, Bidirectional: true})
if err != nil {

return nil, errors.Wrapf(err, "can't create fstrm encorder, address: %s", o.config.GetAddress())
return nil, fmt.Errorf("failed to create fstrm encorder, address: %s err: %w", o.config.GetAddress(), err)
}
return enc, nil
}
14 changes: 8 additions & 6 deletions dnstap_fstrm_unix_socket_input.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,33 @@
package dtap

import (
"fmt"
"net"
"os"
"os/user"
"strconv"

"github.com/pkg/errors"
)

func NewDnstapFstrmUnixSocketInput(config *InputUnixSocketConfig) (*DnstapFstrmSocketInput, error) {
os.Remove(config.GetPath())
l, err := net.Listen("unix", config.GetPath())
if err != nil {
return nil, errors.Wrapf(err, "can't listen %s", config.GetPath())
return nil, fmt.Errorf("failed to listen %s: %w", config.GetPath(), err)
}
if config.GetUser() != "" {
if u, err := user.Lookup(config.GetUser()); err != nil {
return nil, errors.Wrapf(err, "can't get chown user %s", config.GetUser())
return nil, fmt.Errorf("failed to get chown user %s: %w", config.GetUser(), err)
} else {
uid, err := strconv.Atoi(u.Uid)
if err != nil {
return nil, errors.Wrapf(err, "can't chown this system")
return nil, fmt.Errorf("failed to get uid: %w", err)
}
gid, err := strconv.Atoi(u.Gid)
if err != nil {
return nil, fmt.Errorf("failed to get gid: %w", err)
}
if err := os.Chown(config.GetPath(), uid, gid); err != nil {
return nil, errors.Wrapf(err, "can't chown user %s (%s:%s)", config.GetUser(), u.Uid, u.Gid)
return nil, fmt.Errorf("failed to change owner %s (%s:%s): %w", config.GetUser(), u.Uid, u.Gid, err)
}
}
}
Expand Down
9 changes: 3 additions & 6 deletions dnstap_fstrm_unix_socket_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
package dtap

import (
"fmt"
"net"

"github.com/pkg/errors"

dnstap "github.com/dnstap/golang-dnstap"
framestream "github.com/farsightsec/golang-framestream"
)
Expand All @@ -39,13 +38,11 @@ func NewDnstapFstrmUnixSockOutput(config *OutputUnixSocketConfig, params *Dnstap
func (o *DnstapFstrmUnixSockOutput) newConnect() (*framestream.Encoder, error) {
w, err := net.Dial("unix", o.config.GetPath())
if err != nil {

return nil, errors.Wrapf(err, "can't connect unix socket, path: %s", o.config.GetPath())
return nil, fmt.Errorf("failed to connect unix socket, path: %s: %w", o.config.GetPath(), err)
}
enc, err := framestream.NewEncoder(w, &framestream.EncoderOptions{ContentType: dnstap.FSContentType, Bidirectional: true})
if err != nil {

return nil, errors.Wrapf(err, "can't create fstrm encorder, path: %s", o.config.GetPath())
return nil, fmt.Errorf("failed to create fstrm encorder, path: %s: %w", o.config.GetPath(), err)
}
return enc, nil
}
8 changes: 4 additions & 4 deletions dnstap_kafka_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package dtap
import (
"encoding/binary"
"encoding/json"
"fmt"
"io/ioutil"

"github.com/dangkaka/go-kafka-avro"
Expand All @@ -29,7 +30,6 @@ import (
dnstap "github.com/dnstap/golang-dnstap"
"github.com/golang/protobuf/proto"
_ "github.com/mimuret/dtap/statik"
"github.com/pkg/errors"
)

var schemaStr string
Expand Down Expand Up @@ -85,14 +85,14 @@ func (o *DnstapKafkaOutput) open() error {
var err error
o.producer, err = sarama.NewSyncProducer(o.config.Hosts, o.kafkaConfig)
if err != nil {
return errors.Wrapf(err, "can't create kafka producer")
return fmt.Errorf("failed to create kafka producer: %w", err)
}
if o.config.GetOutputType() == "avro" {
if o.valueSchemaID, err = o.getSchemaID(o.config.GetTopic()+"-value", o.valueCodec); err != nil {
return errors.Wrapf(err, "can't get schema id")
return fmt.Errorf("failed to get schema id: %w", err)
}
if o.keySchemaID, err = o.getSchemaID(o.config.GetTopic()+"-key", o.keyCodec); err != nil {
return errors.Wrapf(err, "can't get schema id")
return fmt.Errorf("failed to get schema id: %w", err)
}
}
return nil
Expand Down
Loading

0 comments on commit afa02ac

Please sign in to comment.