From 758038a6f3676beaa11d9a1d435fc8366d8fb380 Mon Sep 17 00:00:00 2001 From: Steve Coffman Date: Sun, 12 Mar 2023 17:50:50 -0400 Subject: [PATCH] Add Go 1.20 errors.Join support Signed-off-by: Steve Coffman --- go.mod | 2 +- join.go | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 join.go diff --git a/go.mod b/go.mod index 5c32a5d..defdc21 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/cockroachdb/errors -go 1.17 +go 1.20 require ( github.com/cockroachdb/datadriven v1.0.2 diff --git a/join.go b/join.go new file mode 100644 index 0000000..30f78a5 --- /dev/null +++ b/join.go @@ -0,0 +1,63 @@ +package errors + +import ( + "fmt" +) + +// 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 +} + +func (e *joinError) Format(w fmt.State, verb rune) { + for i, err := range e.Unwrap() { + if i > 0 { + fmt.Fprint(w, "\n") + } + fmt.Fprintf(w, "[%d]:\n ", i) + s := fmt.Sprintf(fmt.FormatString(w, verb), err) + s = strings.Join(strings.Split(s, "\n"), "\n ") + fmt.Fprint(w, s) + } +}