Skip to content

Commit

Permalink
FanoutHandler: prevent mulitple wrapping with errors.Join (#10)
Browse files Browse the repository at this point in the history
In `Handle`, invoke `errors.Join` just once so errors are not wrapped multiple times.

See: [golang/go#60209](golang/go#60209)
  • Loading branch information
ItalyPaleAle authored Aug 25, 2024
1 parent ced8470 commit 9d82c2e
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions multi.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,21 @@ func (h *FanoutHandler) Enabled(ctx context.Context, l slog.Level) bool {

// Implements slog.Handler
func (h *FanoutHandler) Handle(ctx context.Context, r slog.Record) error {
var result error
// We initialize this with a capacity of 0 to optimize for the path where there are no errors
errs := make([]error, 0, 0)

Check failure on line 39 in multi.go

View workflow job for this annotation

GitHub Actions / lint

S1019: should use make([]error, 0) instead (gosimple)
for i := range h.handlers {
if h.handlers[i].Enabled(ctx, r.Level) {
err := try(func() error {
return h.handlers[i].Handle(ctx, r.Clone())
})
result = errors.Join(result, err)
if err != nil {
errs = append(errs, err)
}
}
}

return result
// If errs is empty, or contains only nil errors, this returns nil
return errors.Join(errs...)
}

// Implements slog.Handler
Expand Down

0 comments on commit 9d82c2e

Please sign in to comment.