diff --git a/integrations/lokiunifi/.travis.yml b/integrations/lokiunifi/.travis.yml index 654efb02..b8e3b955 100644 --- a/integrations/lokiunifi/.travis.yml +++ b/integrations/lokiunifi/.travis.yml @@ -1,9 +1,9 @@ language: go go: -- 1.14.x +- 1.15.x before_install: # download super-linter: golangci-lint - curl -sL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin latest script: -- golangci-lint run --enable-all +- golangci-lint run --enable-all -D exhaustivestruct,nlreturn - go test ./... diff --git a/integrations/lokiunifi/client.go b/integrations/lokiunifi/client.go index 1d1664b0..6c320977 100644 --- a/integrations/lokiunifi/client.go +++ b/integrations/lokiunifi/client.go @@ -8,17 +8,13 @@ import ( "io/ioutil" "net/http" "strings" - - "github.com/pkg/errors" ) const ( lokiPushPath = "/loki/api/v1/push" ) -var ( - errStatusCode = fmt.Errorf("unexpected HTTP status code") -) +var errStatusCode = fmt.Errorf("unexpected HTTP status code") type Client struct { *Config @@ -43,7 +39,7 @@ func (l *Loki) httpClient() *Client { func (c *Client) Post(logs interface{}) error { msg, err := json.Marshal(logs) if err != nil { - return err + return fmt.Errorf("json marshal: %w", err) } u := strings.TrimSuffix(c.URL, lokiPushPath) + lokiPushPath @@ -59,7 +55,7 @@ func (c *Client) Post(logs interface{}) error { m := fmt.Sprintf("%s (%d/%s) %s, msg: %s", u, code, http.StatusText(code), strings.TrimSpace(strings.ReplaceAll(string(body), "\n", " ")), msg) - return errors.Wrap(errStatusCode, m) + return fmt.Errorf("%s: %w", m, errStatusCode) } return nil @@ -67,9 +63,9 @@ func (c *Client) Post(logs interface{}) error { // NewRequest creates the http request based on input data. func (c *Client) NewRequest(url, method, cType string, msg []byte) (*http.Request, error) { - req, err := http.NewRequest(method, url, bytes.NewBuffer(msg)) + req, err := http.NewRequest(method, url, bytes.NewBuffer(msg)) //nolint:noctx if err != nil { - return nil, err + return nil, fmt.Errorf("creating request: %w", err) } if cType != "" { @@ -91,11 +87,14 @@ func (c *Client) NewRequest(url, method, cType string, msg []byte) (*http.Reques func (c *Client) Do(req *http.Request) (int, []byte, error) { resp, err := c.Client.Do(req) if err != nil { - return 0, nil, err + return 0, nil, fmt.Errorf("making request: %w", err) } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return resp.StatusCode, body, fmt.Errorf("reading body: %w", err) + } - return resp.StatusCode, body, err + return resp.StatusCode, body, nil } diff --git a/integrations/lokiunifi/loki.go b/integrations/lokiunifi/loki.go index b6e2fb92..d106d15e 100644 --- a/integrations/lokiunifi/loki.go +++ b/integrations/lokiunifi/loki.go @@ -1,11 +1,11 @@ package lokiunifi import ( + "fmt" "io/ioutil" "strings" "time" - "github.com/pkg/errors" "github.com/unifi-poller/poller" "golift.io/cnfg" ) @@ -126,7 +126,7 @@ func (l *Loki) ProcessEvents(report *Report, events *poller.Events) error { logs := report.ProcessEventLogs(events) if err := l.client.Post(logs); err != nil { - return errors.Wrap(err, "sending to Loki failed") + return fmt.Errorf("sending to Loki failed: %w", err) } l.last = report.Start