github.com/quickfeed/quickfeed@v0.0.0-20240507093252-ed8ca812a09c/internal/multierr/multierr.go (about) 1 package multierr 2 3 import ( 4 "fmt" 5 "strings" 6 ) 7 8 type multiErr []error 9 10 func (errs multiErr) Error() string { 11 var b strings.Builder 12 if len(errs) == 1 { 13 return errs[0].Error() 14 } 15 for _, e := range errs { 16 fmt.Fprintf(&b, "%s\n", e) 17 } 18 return b.String() 19 } 20 21 func Join(errs ...error) error { 22 err := make(multiErr, 0, len(errs)) 23 for _, e := range errs { 24 if e == nil { 25 continue 26 } 27 err = append(err, e) 28 } 29 if len(err) == 0 { 30 return nil 31 } 32 return err 33 }