From e8a20f587c0b0129bc68f5c6092c54f2b4c3519a Mon Sep 17 00:00:00 2001 From: tobi <31960611+tsmethurst@users.noreply.github.com> Date: Wed, 2 Aug 2023 17:21:46 +0200 Subject: [bugfix] Rework MultiError to wrap + unwrap errors properly (#2057) * rework multierror a bit * test multierror --- internal/gtserror/multi.go | 51 +++++++++++++++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 14 deletions(-) (limited to 'internal/gtserror/multi.go') diff --git a/internal/gtserror/multi.go b/internal/gtserror/multi.go index 371eb2f63..1c533b285 100644 --- a/internal/gtserror/multi.go +++ b/internal/gtserror/multi.go @@ -20,25 +20,48 @@ package gtserror import ( "errors" "fmt" - "strings" ) -// MultiError allows encapsulating multiple errors under a singular instance, -// which is useful when you only want to log on errors, not return early / bubble up. -type MultiError []string +// MultiError allows encapsulating multiple +// errors under a singular instance, which +// is useful when you only want to log on +// errors, not return early / bubble up. +type MultiError struct { + e []error +} -func (e *MultiError) Append(err error) { - *e = append(*e, err.Error()) +// NewMultiError returns a *MultiError with +// the capacity of its underlying error slice +// set to the provided value. +// +// This capacity can be exceeded if necessary, +// but it saves a teeny tiny bit of memory if +// callers set it correctly. +// +// If you don't know in advance what the capacity +// must be, just use new(MultiError) instead. +func NewMultiError(capacity int) *MultiError { + return &MultiError{ + e: make([]error, 0, capacity), + } } -func (e *MultiError) Appendf(format string, args ...any) { - *e = append(*e, fmt.Sprintf(format, args...)) +// Append the given error to the MultiError. +func (m *MultiError) Append(err error) { + m.e = append(m.e, err) } -// Combine converts this multiError to a singular error instance, returning nil if empty. -func (e MultiError) Combine() error { - if len(e) == 0 { - return nil - } - return errors.New(`"` + strings.Join(e, `","`) + `"`) +// Append the given format string to the MultiError. +// +// It is valid to use %w in the format string +// to wrap any other errors. +func (m *MultiError) Appendf(format string, args ...any) { + m.e = append(m.e, fmt.Errorf(format, args...)) +} + +// Combine the MultiError into a single error. +// +// Unwrap will work on the returned error as expected. +func (m MultiError) Combine() error { + return errors.Join(m.e...) } -- cgit v1.2.3