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

This commit is contained in:
Carl Bergquist 2019-05-15 12:20:17 +02:00 committed by GitHub
parent fdd421e24c
commit c55e6016bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 42 additions and 22 deletions

2
go.mod
View File

@ -48,7 +48,7 @@ require (
github.com/onsi/gomega v1.5.0 // indirect github.com/onsi/gomega v1.5.0 // indirect
github.com/opentracing/opentracing-go v1.1.0 github.com/opentracing/opentracing-go v1.1.0
github.com/patrickmn/go-cache v2.1.0+incompatible 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_golang v0.9.2
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90 github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90
github.com/prometheus/common v0.2.0 github.com/prometheus/common v0.2.0

View File

@ -12,9 +12,9 @@ import (
"net" "net"
"strconv" "strconv"
m "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/setting" "github.com/grafana/grafana/pkg/setting"
"github.com/pkg/errors" "github.com/grafana/grafana/pkg/util/errutil"
gomail "gopkg.in/mail.v2" gomail "gopkg.in/mail.v2"
) )
@ -38,11 +38,11 @@ func (ns *NotificationService) send(msg *Message) (int, error) {
e := dialer.DialAndSend(m) e := dialer.DialAndSend(m)
if e != nil { 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 continue
} }
num += 1 num++
} }
return num, err return num, err
@ -83,9 +83,9 @@ func (ns *NotificationService) createDialer() (*gomail.Dialer, error) {
return d, nil 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 { if !ns.Cfg.Smtp.Enabled {
return nil, m.ErrSmtpNotEnabled return nil, models.ErrSmtpNotEnabled
} }
var buffer bytes.Buffer var buffer bytes.Buffer

View File

@ -3,8 +3,9 @@ package dashboards
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/infra/log"
"github.com/pkg/errors" "github.com/grafana/grafana/pkg/util/errutil"
) )
type DashboardProvisionerImpl struct { type DashboardProvisionerImpl struct {
@ -18,13 +19,13 @@ func NewDashboardProvisionerImpl(configDirectory string) (*DashboardProvisionerI
configs, err := cfgReader.readConfig() configs, err := cfgReader.readConfig()
if err != nil { 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) fileReaders, err := getFileReaders(configs, logger)
if err != nil { 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{ d := &DashboardProvisionerImpl{
@ -39,7 +40,7 @@ func (provider *DashboardProvisionerImpl) Provision() error {
for _, reader := range provider.fileReaders { for _, reader := range provider.fileReaders {
err := reader.startWalkingDisk() err := reader.startWalkingDisk()
if err != nil { 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": case "file":
fileReader, err := NewDashboardFileReader(config, logger.New("type", config.Type, "name", config.Name)) fileReader, err := NewDashboardFileReader(config, logger.New("type", config.Type, "name", config.Name))
if err != nil { 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) readers = append(readers, fileReader)
default: default:

View File

@ -6,7 +6,7 @@ import (
"sync" "sync"
"github.com/grafana/grafana/pkg/infra/log" "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/registry"
"github.com/grafana/grafana/pkg/services/provisioning/dashboards" "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 { func (ps *provisioningServiceImpl) ProvisionDatasources() error {
datasourcePath := path.Join(ps.Cfg.ProvisioningPath, "datasources") datasourcePath := path.Join(ps.Cfg.ProvisioningPath, "datasources")
err := ps.provisionDatasources(datasourcePath) err := ps.provisionDatasources(datasourcePath)
return errors.Wrap(err, "Datasource provisioning error") return errutil.Wrap("Datasource provisioning error", err)
} }
func (ps *provisioningServiceImpl) ProvisionNotifications() error { func (ps *provisioningServiceImpl) ProvisionNotifications() error {
alertNotificationsPath := path.Join(ps.Cfg.ProvisioningPath, "notifiers") alertNotificationsPath := path.Join(ps.Cfg.ProvisioningPath, "notifiers")
err := ps.provisionNotifiers(alertNotificationsPath) 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 { func (ps *provisioningServiceImpl) ProvisionDashboards() error {
dashboardPath := path.Join(ps.Cfg.ProvisioningPath, "dashboards") dashboardPath := path.Join(ps.Cfg.ProvisioningPath, "dashboards")
dashProvisioner, err := ps.newDashboardProvisioner(dashboardPath) dashProvisioner, err := ps.newDashboardProvisioner(dashboardPath)
if err != nil { if err != nil {
return errors.Wrap(err, "Failed to create provisioner") return errutil.Wrap("Failed to create provisioner", err)
} }
ps.mutex.Lock() ps.mutex.Lock()
@ -127,7 +127,7 @@ func (ps *provisioningServiceImpl) ProvisionDashboards() error {
if err := dashProvisioner.Provision(); err != nil { 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 // 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. // 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 ps.dashboardProvisioner = dashProvisioner
return nil return nil

View File

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

View File

@ -1,11 +1,29 @@
package errutil 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 // 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. // 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 // 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. // wrapper so hopefully it will be added in the standard lib later.
func Wrap(message string, err error) error { func Wrap(message string, err error) error {
if err == nil {
return nil
}
return xerrors.Errorf("%v: %w", message, err) 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)
}

View File

@ -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) [![Build Status](https://travis-ci.org/robfig/cron.svg?branch=master)](https://travis-ci.org/robfig/cron)
# cron # cron

View File

@ -84,7 +84,7 @@ You may use one of several pre-defined schedules in place of a cron expression.
Intervals 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: or cron is run. This is supported by formatting the cron spec like this:
@every <duration> @every <duration>