From 3d8b5e89aeaa7642a9365ac8dedae977876367ea Mon Sep 17 00:00:00 2001 From: Oleg Zaytsev Date: Wed, 22 Mar 2023 17:29:39 +0100 Subject: [PATCH] Implement Is() for multierrors We need to be able to identify the errors that a multierror wraps. Especially in case of `context.Canceled`. This is the same implementation of that method as we do in github.com/grafana/dskit/multierror Signed-off-by: Oleg Zaytsev --- tsdb/errors/errors.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tsdb/errors/errors.go b/tsdb/errors/errors.go index 607a7782a..aa0a4b1b3 100644 --- a/tsdb/errors/errors.go +++ b/tsdb/errors/errors.go @@ -16,6 +16,7 @@ package errors import ( "bytes" + "errors" "fmt" "io" ) @@ -79,6 +80,19 @@ func (es nonNilMultiError) Error() string { return buf.String() } +// Is attempts to match the provided error against errors in the error list. +// +// This function allows errors.Is to traverse the values stored in the MultiError. +// It returns true if any of the errors in the list match the target. +func (es nonNilMultiError) Is(target error) bool { + for _, err := range es.errs { + if errors.Is(err, target) { + return true + } + } + return false +} + // CloseAll closes all given closers while recording error in MultiError. func CloseAll(cs []io.Closer) error { errs := NewMulti()