Chore: Replace go-multierror with errors package (#66432)

* code refactor and type assertions added to tests

* no-lint rule added for specific line
This commit is contained in:
SatVeer Singh 2023-06-19 14:59:45 +05:30 committed by GitHub
parent a75752b085
commit 1bfa3a0f1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 25 additions and 30 deletions

2
go.mod
View File

@ -176,7 +176,7 @@ require (
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.1-0.20191002090509-6af20e3a5340 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-msgpack v0.5.5 // indirect
github.com/hashicorp/go-multierror v1.1.1
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-sockaddr v1.0.2 // indirect
github.com/hashicorp/golang-lru v0.6.0 // indirect
github.com/hashicorp/yamux v0.1.1 // indirect

View File

@ -6,7 +6,6 @@ import (
"net/http"
"strconv"
"github.com/hashicorp/go-multierror"
"github.com/prometheus/client_golang/prometheus"
"go.opentelemetry.io/otel/attribute"
@ -143,7 +142,7 @@ func ProvideService(
connector, errConnector := socialService.GetConnector(name)
httpClient, errHTTPClient := socialService.GetOAuthHttpClient(name)
if errConnector != nil || errHTTPClient != nil {
s.log.Error("Failed to configure oauth client", "client", clientName, "err", multierror.Append(errConnector, errHTTPClient))
s.log.Error("Failed to configure oauth client", "client", clientName, "err", errors.Join(errConnector, errHTTPClient))
} else {
s.RegisterClient(clients.ProvideOAuth(clientName, cfg, oauthCfg, connector, httpClient))
}
@ -201,7 +200,7 @@ func (s *Service) Authenticate(ctx context.Context, r *authn.Request) (*authn.Id
return nil, err
}
authErr = multierror.Append(authErr, err)
authErr = errors.Join(authErr, err)
// try next
continue
}

View File

@ -4,8 +4,6 @@ import (
"context"
"errors"
"github.com/hashicorp/go-multierror"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/services/authn"
"github.com/grafana/grafana/pkg/services/loginattempt"
@ -50,7 +48,7 @@ func (c *Password) AuthenticatePassword(ctx context.Context, r *authn.Request, u
var clientErrs error
for _, pwClient := range c.clients {
identity, clientErr := pwClient.AuthenticatePassword(ctx, r, username, password)
clientErrs = multierror.Append(clientErrs, clientErr)
clientErrs = errors.Join(clientErrs, clientErr)
// we always try next client on any error
if clientErr != nil {
c.log.FromContext(ctx).Debug("Failed to authenticate password identity", "client", pwClient, "error", clientErr)

View File

@ -10,7 +10,6 @@ import (
"github.com/benbjohnson/clock"
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/hashicorp/go-multierror"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/services/ngalert/eval"
@ -72,7 +71,7 @@ func (e *Engine) Test(ctx context.Context, user *user.SignedInUser, rule *models
evaluator, err := backtestingEvaluatorFactory(ruleCtx, e.evalFactory, user, rule.GetEvalCondition())
if err != nil {
return nil, multierror.Append(ErrInvalidInputData, err)
return nil, errors.Join(ErrInvalidInputData, err)
}
stateManager := e.createStateManager()

View File

@ -8,7 +8,6 @@ import (
"time"
"github.com/benbjohnson/clock"
"github.com/hashicorp/go-multierror"
"go.opentelemetry.io/otel/attribute"
"golang.org/x/sync/errgroup"
@ -399,7 +398,7 @@ func (sch *schedule) ruleRoutine(grafanaCtx context.Context, key ngmodels.AlertR
if err == nil {
for _, result := range results {
if result.Error != nil {
err = multierror.Append(err, result.Error)
err = errors.Join(err, result.Error)
}
}
}

View File

@ -2,6 +2,7 @@ package state
import (
"context"
"errors"
"math"
"net/url"
"strings"
@ -9,7 +10,6 @@ import (
"time"
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/hashicorp/go-multierror"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/services/ngalert/eval"
@ -161,21 +161,21 @@ func calculateState(ctx context.Context, log log.Logger, alertRule *ngModels.Ale
// template.ExpandError errors.
func expand(ctx context.Context, log log.Logger, name string, original map[string]string, data template.Data, externalURL *url.URL, evaluatedAt time.Time) (map[string]string, error) {
var (
errs *multierror.Error
errs error
expanded = make(map[string]string, len(original))
)
for k, v := range original {
result, err := template.Expand(ctx, name, v, data, externalURL, evaluatedAt)
if err != nil {
log.Error("Error in expanding template", "error", err)
errs = multierror.Append(errs, err)
errs = errors.Join(errs, err)
// keep the original template on error
expanded[k] = v
} else {
expanded[k] = result
}
}
return expanded, errs.ErrorOrNil()
return expanded, errs
}
func (rs *ruleStates) deleteStates(predicate func(s *State) bool) []*State {

View File

@ -9,7 +9,6 @@ import (
"time"
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/hashicorp/go-multierror"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@ -67,14 +66,15 @@ func Test_expand(t *testing.T) {
require.NotNil(t, err)
require.Equal(t, original, results)
// TODO: Please update this test in issue https://github.com/grafana/grafana/issues/63686
var multierr *multierror.Error
require.True(t, errors.As(err, &multierr))
require.Equal(t, multierr.Len(), 2)
//nolint:errorlint
multierr, is := err.(interface{ Unwrap() []error })
require.True(t, is)
unwrappedErrors := multierr.Unwrap()
require.Equal(t, len(unwrappedErrors), 2)
errsStr := []string{
multierr.Errors[0].Error(),
multierr.Errors[1].Error(),
unwrappedErrors[0].Error(),
unwrappedErrors[1].Error(),
}
firstErrStr := "failed to expand template '{{- $labels := .Labels -}}{{- $values := .Values -}}{{- $value := .Value -}}Instance {{ $labels. }} has been down for more than 5 minutes': error parsing template __alert_test: template: __alert_test:1: unexpected <.> in operand"
@ -83,7 +83,7 @@ func Test_expand(t *testing.T) {
require.Contains(t, errsStr, firstErrStr)
require.Contains(t, errsStr, secondErrStr)
for _, err := range multierr.Errors {
for _, err := range unwrappedErrors {
var expandErr template.ExpandError
require.True(t, errors.As(err, &expandErr))
}
@ -103,10 +103,11 @@ func Test_expand(t *testing.T) {
require.NotNil(t, err)
require.Equal(t, expected, results)
// TODO: Please update this test in issue https://github.com/grafana/grafana/issues/63686
var multierr *multierror.Error
require.True(t, errors.As(err, &multierr))
require.Equal(t, multierr.Len(), 1)
//nolint:errorlint
multierr, is := err.(interface{ Unwrap() []error })
require.True(t, is)
unwrappedErrors := multierr.Unwrap()
require.Equal(t, len(unwrappedErrors), 1)
// assert each error matches the expected error
var expandErr template.ExpandError

View File

@ -2,9 +2,8 @@ package util
import (
"context"
"errors"
"sync"
"github.com/hashicorp/go-multierror"
)
// this is a decorator for a regular context that contains a custom error and returns the
@ -18,7 +17,7 @@ func (c *contextWithCancellableReason) Err() error {
c.mtx.Lock()
defer c.mtx.Unlock()
if c.err != nil {
return multierror.Append(c.Context.Err(), c.err)
return errors.Join(c.Context.Err(), c.err)
}
return c.Context.Err()
}