Skip to content

Commit

Permalink
Multierror: join errors at the end (#132)
Browse files Browse the repository at this point in the history
This fixes `MapErr` so that it does not create recursive `joinError`s.
By repeatedly calling `errors.Join(`, we create a nested set of
`joinError`s rather than a single, flat `joinError`. Then, when
`Error()` is called, it allocates a new string for each nested error
because `joinError` calls `Error()` recursively on each of its children.

Instead, this PR updates `MapErr` to just collect a slice of errors and
return the flat joined error.
  • Loading branch information
camdencheek committed Jan 8, 2024
1 parent 30a99cd commit 4afefce
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions iter/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,16 @@ func (m Mapper[T, R]) MapErr(input []T, f func(*T) (R, error)) ([]R, error) {
var (
res = make([]R, len(input))
errMux sync.Mutex
errs error
errs []error
)
Iterator[T](m).ForEachIdx(input, func(i int, t *T) {
var err error
res[i], err = f(t)
if err != nil {
errMux.Lock()
errs = multierror.Join(errs, err)
errs = append(errs, err)
errMux.Unlock()
}
})
return res, errs
return res, multierror.Join(errs...)
}

0 comments on commit 4afefce

Please sign in to comment.