1 package emperror 2 3 // ForEachCause loops through an error chain and calls a function for each of them, 4 // starting with the topmost one. 5 // 6 // The function can return false to break the loop before it ends. 7 func ForEachCause(err error, fn func(err error) bool) { 8 // causer is the interface defined in github.com/pkg/errors for specifying a parent error. 9 type causer interface { 10 Cause() error 11 } 12 13 for err != nil { 14 continueLoop := fn(err) 15 if !continueLoop { 16 break 17 } 18 19 cause, ok := err.(causer) 20 if !ok { 21 break 22 } 23 24 err = cause.Cause() 25 } 26 } 27