Browse Source

backend: replace /pkg/errors with errutil (#17065)

Carl Bergquist 6 years ago
parent
commit
c55e6016bf

+ 1 - 1
go.mod

@@ -48,7 +48,7 @@ require (
 	github.com/onsi/gomega v1.5.0 // indirect
 	github.com/opentracing/opentracing-go v1.1.0
 	github.com/patrickmn/go-cache v2.1.0+incompatible
-	github.com/pkg/errors v0.8.1
+	github.com/pkg/errors v0.8.1 // indirect
 	github.com/prometheus/client_golang v0.9.2
 	github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90
 	github.com/prometheus/common v0.2.0

+ 6 - 6
pkg/services/notifications/mailer.go

@@ -12,9 +12,9 @@ import (
 	"net"
 	"strconv"
 
-	m "github.com/grafana/grafana/pkg/models"
+	"github.com/grafana/grafana/pkg/models"
 	"github.com/grafana/grafana/pkg/setting"
-	"github.com/pkg/errors"
+	"github.com/grafana/grafana/pkg/util/errutil"
 	gomail "gopkg.in/mail.v2"
 )
 
@@ -38,11 +38,11 @@ func (ns *NotificationService) send(msg *Message) (int, error) {
 
 		e := dialer.DialAndSend(m)
 		if e != nil {
-			err = errors.Wrapf(e, "Failed to send notification to email address: %s", address)
+			err = errutil.Wrapf(e, "Failed to send notification to email address: %s", address)
 			continue
 		}
 
-		num += 1
+		num++
 	}
 
 	return num, err
@@ -83,9 +83,9 @@ func (ns *NotificationService) createDialer() (*gomail.Dialer, error) {
 	return d, nil
 }
 
-func (ns *NotificationService) buildEmailMessage(cmd *m.SendEmailCommand) (*Message, error) {
+func (ns *NotificationService) buildEmailMessage(cmd *models.SendEmailCommand) (*Message, error) {
 	if !ns.Cfg.Smtp.Enabled {
-		return nil, m.ErrSmtpNotEnabled
+		return nil, models.ErrSmtpNotEnabled
 	}
 
 	var buffer bytes.Buffer

+ 6 - 5
pkg/services/provisioning/dashboards/dashboard.go

@@ -3,8 +3,9 @@ package dashboards
 import (
 	"context"
 	"fmt"
+
 	"github.com/grafana/grafana/pkg/infra/log"
-	"github.com/pkg/errors"
+	"github.com/grafana/grafana/pkg/util/errutil"
 )
 
 type DashboardProvisionerImpl struct {
@@ -18,13 +19,13 @@ func NewDashboardProvisionerImpl(configDirectory string) (*DashboardProvisionerI
 	configs, err := cfgReader.readConfig()
 
 	if err != nil {
-		return nil, errors.Wrap(err, "Failed to read dashboards config")
+		return nil, errutil.Wrap("Failed to read dashboards config", err)
 	}
 
 	fileReaders, err := getFileReaders(configs, logger)
 
 	if err != nil {
-		return nil, errors.Wrap(err, "Failed to initialize file readers")
+		return nil, errutil.Wrap("Failed to initialize file readers", err)
 	}
 
 	d := &DashboardProvisionerImpl{
@@ -39,7 +40,7 @@ func (provider *DashboardProvisionerImpl) Provision() error {
 	for _, reader := range provider.fileReaders {
 		err := reader.startWalkingDisk()
 		if err != nil {
-			return errors.Wrapf(err, "Failed to provision config %v", reader.Cfg.Name)
+			return errutil.Wrapf(err, "Failed to provision config %v", reader.Cfg.Name)
 		}
 	}
 
@@ -73,7 +74,7 @@ func getFileReaders(configs []*DashboardsAsConfig, logger log.Logger) ([]*fileRe
 		case "file":
 			fileReader, err := NewDashboardFileReader(config, logger.New("type", config.Type, "name", config.Name))
 			if err != nil {
-				return nil, errors.Wrapf(err, "Failed to create file reader for config %v", config.Name)
+				return nil, errutil.Wrapf(err, "Failed to create file reader for config %v", config.Name)
 			}
 			readers = append(readers, fileReader)
 		default:

+ 5 - 5
pkg/services/provisioning/provisioning.go

@@ -6,7 +6,7 @@ import (
 	"sync"
 
 	"github.com/grafana/grafana/pkg/infra/log"
-	"github.com/pkg/errors"
+	"github.com/grafana/grafana/pkg/util/errutil"
 
 	"github.com/grafana/grafana/pkg/registry"
 	"github.com/grafana/grafana/pkg/services/provisioning/dashboards"
@@ -103,20 +103,20 @@ func (ps *provisioningServiceImpl) Run(ctx context.Context) error {
 func (ps *provisioningServiceImpl) ProvisionDatasources() error {
 	datasourcePath := path.Join(ps.Cfg.ProvisioningPath, "datasources")
 	err := ps.provisionDatasources(datasourcePath)
-	return errors.Wrap(err, "Datasource provisioning error")
+	return errutil.Wrap("Datasource provisioning error", err)
 }
 
 func (ps *provisioningServiceImpl) ProvisionNotifications() error {
 	alertNotificationsPath := path.Join(ps.Cfg.ProvisioningPath, "notifiers")
 	err := ps.provisionNotifiers(alertNotificationsPath)
-	return errors.Wrap(err, "Alert notification provisioning error")
+	return errutil.Wrap("Alert notification provisioning error", err)
 }
 
 func (ps *provisioningServiceImpl) ProvisionDashboards() error {
 	dashboardPath := path.Join(ps.Cfg.ProvisioningPath, "dashboards")
 	dashProvisioner, err := ps.newDashboardProvisioner(dashboardPath)
 	if err != nil {
-		return errors.Wrap(err, "Failed to create provisioner")
+		return errutil.Wrap("Failed to create provisioner", err)
 	}
 
 	ps.mutex.Lock()
@@ -127,7 +127,7 @@ func (ps *provisioningServiceImpl) ProvisionDashboards() error {
 	if err := dashProvisioner.Provision(); err != nil {
 		// If we fail to provision with the new provisioner, mutex will unlock and the polling we restart with the
 		// old provisioner as we did not switch them yet.
-		return errors.Wrap(err, "Failed to provision dashboards")
+		return errutil.Wrap("Failed to provision dashboards", err)
 	}
 	ps.dashboardProvisioner = dashProvisioner
 	return nil

+ 3 - 2
pkg/services/provisioning/values/values.go

@@ -11,10 +11,11 @@
 package values
 
 import (
-	"github.com/pkg/errors"
 	"os"
 	"reflect"
 	"strconv"
+
+	"github.com/grafana/grafana/pkg/util/errutil"
 )
 
 type IntValue struct {
@@ -33,7 +34,7 @@ func (val *IntValue) UnmarshalYAML(unmarshal func(interface{}) error) error {
 	}
 	val.Raw = interpolated.raw
 	val.value, err = strconv.Atoi(interpolated.value)
-	return errors.Wrap(err, "cannot convert value int")
+	return errutil.Wrap("cannot convert value int", err)
 }
 
 func (val *IntValue) Value() int {

+ 19 - 1
pkg/util/errutil/errors.go

@@ -1,11 +1,29 @@
 package errutil
 
-import "golang.org/x/xerrors"
+import (
+	"fmt"
+
+	"golang.org/x/xerrors"
+)
 
 // Wrap is a simple wrapper around Errorf that is doing error wrapping. You can read how that works in
 // https://godoc.org/golang.org/x/xerrors#Errorf but its API is very implicit which is a reason for this wrapper.
 // There is also a discussion (https://github.com/golang/go/issues/29934) where many comments make arguments for such
 // wrapper so hopefully it will be added in the standard lib later.
 func Wrap(message string, err error) error {
+	if err == nil {
+		return nil
+	}
+
 	return xerrors.Errorf("%v: %w", message, err)
 }
+
+// Wrapf is a simple wrapper around Errorf that is doing error wrapping
+// Wrapf allows you to send a format and args instead of just a message.
+func Wrapf(err error, message string, a ...interface{}) error {
+	if err == nil {
+		return nil
+	}
+
+	return Wrap(fmt.Sprintf(message, a...), err)
+}

+ 1 - 1
vendor/github.com/robfig/cron/README.md

@@ -1,4 +1,4 @@
-[![GoDoc](http://godoc.org/github.com/robfig/cron?status.png)](http://godoc.org/github.com/robfig/cron)
+[![GoDoc](http://godoc.org/github.com/robfig/cron?status.png)](http://godoc.org/github.com/robfig/cron) 
 [![Build Status](https://travis-ci.org/robfig/cron.svg?branch=master)](https://travis-ci.org/robfig/cron)
 
 # cron

+ 1 - 1
vendor/github.com/robfig/cron/doc.go

@@ -84,7 +84,7 @@ You may use one of several pre-defined schedules in place of a cron expression.
 
 Intervals
 
-You may also schedule a job to execute at fixed intervals, starting at the time it's added
+You may also schedule a job to execute at fixed intervals, starting at the time it's added 
 or cron is run. This is supported by formatting the cron spec like this:
 
     @every <duration>