mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Vendor errors.Join from Go standard library to avoid version incompatibilities (#64985)
Vendor errors.Join from std lib
This commit is contained in:
parent
a31672fa40
commit
40c5713cbd
@ -2,7 +2,6 @@ package historian
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
|
|
||||||
"github.com/grafana/grafana-plugin-sdk-go/data"
|
"github.com/grafana/grafana-plugin-sdk-go/data"
|
||||||
ngmodels "github.com/grafana/grafana/pkg/services/ngalert/models"
|
ngmodels "github.com/grafana/grafana/pkg/services/ngalert/models"
|
||||||
@ -45,7 +44,7 @@ func (h *MultipleBackend) Record(ctx context.Context, rule history_model.RuleMet
|
|||||||
errs = append(errs, err)
|
errs = append(errs, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
errCh <- errors.Join(errs...)
|
errCh <- Join(errs...)
|
||||||
}()
|
}()
|
||||||
return errCh
|
return errCh
|
||||||
}
|
}
|
||||||
@ -53,3 +52,53 @@ func (h *MultipleBackend) Record(ctx context.Context, rule history_model.RuleMet
|
|||||||
func (h *MultipleBackend) Query(ctx context.Context, query ngmodels.HistoryQuery) (*data.Frame, error) {
|
func (h *MultipleBackend) Query(ctx context.Context, query ngmodels.HistoryQuery) (*data.Frame, error) {
|
||||||
return h.primary.Query(ctx, query)
|
return h.primary.Query(ctx, query)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: This is vendored verbatim from the Go standard library.
|
||||||
|
// TODO: The grafana project doesn't support go 1.20 yet, so we can't use errors.Join() directly.
|
||||||
|
// TODO: Remove this and replace calls with "errors.Join(...)" when go 1.20 becomes the minimum supported version.
|
||||||
|
//
|
||||||
|
// Join returns an error that wraps the given errors.
|
||||||
|
// Any nil error values are discarded.
|
||||||
|
// Join returns nil if errs contains no non-nil values.
|
||||||
|
// The error formats as the concatenation of the strings obtained
|
||||||
|
// by calling the Error method of each element of errs, with a newline
|
||||||
|
// between each string.
|
||||||
|
func Join(errs ...error) error {
|
||||||
|
n := 0
|
||||||
|
for _, err := range errs {
|
||||||
|
if err != nil {
|
||||||
|
n++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if n == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
e := &joinError{
|
||||||
|
errs: make([]error, 0, n),
|
||||||
|
}
|
||||||
|
for _, err := range errs {
|
||||||
|
if err != nil {
|
||||||
|
e.errs = append(e.errs, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return e
|
||||||
|
}
|
||||||
|
|
||||||
|
type joinError struct {
|
||||||
|
errs []error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *joinError) Error() string {
|
||||||
|
var b []byte
|
||||||
|
for i, err := range e.errs {
|
||||||
|
if i > 0 {
|
||||||
|
b = append(b, '\n')
|
||||||
|
}
|
||||||
|
b = append(b, err.Error()...)
|
||||||
|
}
|
||||||
|
return string(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *joinError) Unwrap() []error {
|
||||||
|
return e.errs
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user