-
Notifications
You must be signed in to change notification settings - Fork 0
/
fender.go
40 lines (34 loc) · 894 Bytes
/
fender.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package fender
import (
"context"
"github.com/foomo/fender/fend"
)
func All(ctx context.Context, fends ...fend.Fend) error {
return Mode(ctx, fend.ModeAll, fends...)
}
func First(ctx context.Context, fends ...fend.Fend) error {
return Mode(ctx, fend.ModeFirst, fends...)
}
func AllFirst(ctx context.Context, fends ...fend.Fend) error {
return Mode(ctx, fend.ModeAllFirst, fends...)
}
func Mode(ctx context.Context, mode fend.Mode, fends ...fend.Fend) error {
var cause []*fend.Error
for _, validator := range fends {
err := validator(ctx, mode)
if e, ok := err.(*fend.Error); ok { //nolint:errorlint
// append error
cause = append(cause, e)
// break if we only want the first errors
if mode == fend.ModeFirst || mode == fend.ModeAllFirst {
break
}
} else if err != nil {
return err
}
}
if cause != nil {
return NewError(cause...)
}
return nil
}