github.com/searKing/golang/go@v1.2.117/errors/handler.go (about) 1 package errors 2 3 import ( 4 "context" 5 "time" 6 7 "golang.org/x/time/rate" 8 ) 9 10 // ErrorHandlers is a list of functions which will be invoked when a nonreturnable 11 // error occurs. 12 // should be packaged up into a testable and reusable object. 13 var ErrorHandlers = []func(error){ 14 func() func(err error) { 15 limiter := rate.NewLimiter(rate.Every(time.Millisecond), 1) 16 return func(err error) { 17 // 1ms was the number folks were able to stomach as a global rate limit. 18 // If you need to log errors more than 1000 times a second you 19 // should probably consider fixing your code instead. :) 20 _ = limiter.Wait(context.Background()) 21 } 22 }(), 23 } 24 25 // HandleError is a method to invoke when a non-user facing piece of code cannot 26 // return an error and needs to indicate it has been ignored. Invoking this method 27 // is preferable to logging the error - the default behavior is to log but the 28 // errors may be sent to a remote server for analysis. 29 func HandleError(err error) { 30 // this is sometimes called with a nil error. We probably shouldn't fail and should do nothing instead 31 if err == nil { 32 return 33 } 34 35 for _, fn := range ErrorHandlers { 36 fn(err) 37 } 38 }