github.com/angenalZZZ/gofunc@v0.0.0-20210507121333-48ff1be3917b/f/error.go (about) 1 package f 2 3 import ( 4 "errors" 5 "fmt" 6 "strings" 7 ) 8 9 // backoff.go 10 // Cannot get results after retrying many times 11 var ( 12 ErrRetryOperationFailure = errors.New("unable to get results after multiple retries") 13 ) 14 15 // validator.go 16 var ( 17 ErrConvertFail = errors.New("convert value is failure") 18 ErrBadComparisonType = errors.New("invalid type for operation") 19 ErrBadInput = errors.New("invalid input for operation") 20 ) 21 22 // goroutine.go 23 // Errors that are used throughout the Tunny API. 24 var ( 25 ErrPoolNotRunning = errors.New("the pool is not running") 26 ErrJobNotFunc = errors.New("generic worker not given a func()") 27 ErrWorkerClosed = errors.New("worker was closed") 28 ErrJobTimedOut = errors.New("job request timed out") 29 ) 30 31 // Errors is an array of multiple errors and conforms to the error interface. 32 type Errors []error 33 34 // Errors returns itself. 35 func (es Errors) Errors() []error { 36 return es 37 } 38 39 func (es Errors) Error() string { 40 var errs []string 41 for _, e := range es { 42 errs = append(errs, e.Error()) 43 } 44 return strings.Join(errs, ";") 45 } 46 47 // Error encapsulates a name, an error and whether there's a custom error message or not. 48 type Error struct { 49 Name string 50 Err error 51 CustomErrorMessageExists bool 52 53 // Validator indicates the name of the validator that failed 54 Validator string 55 Path []string 56 } 57 58 func (e Error) Error() string { 59 if e.CustomErrorMessageExists { 60 return e.Err.Error() 61 } 62 63 errName := e.Name 64 if len(e.Path) > 0 { 65 errName = strings.Join(append(e.Path, e.Name), ".") 66 } 67 68 return errName + ": " + e.Err.Error() 69 } 70 71 // Must not error, or panic. 72 func Must(err error) { 73 if err != nil { 74 panic(err) 75 } 76 } 77 78 // MustBytes bytes length is these numbers, or panic. 79 func MustBytes(p []byte, n ...int) { 80 if n != nil && len(n) > 0 { 81 if p == nil { 82 panic(errors.New("wrong empty bytes")) 83 } 84 ok := false 85 l := len(p) 86 for _, i := range n { 87 if i == l { 88 ok = true 89 break 90 } 91 } 92 if ok == false { 93 panic(errors.New("wrong bytes length")) 94 } 95 } 96 } 97 98 // Panic exit with error. 99 func Panic(format string, args ...interface{}) { 100 if len(args) == 0 { 101 panic(format) 102 } else { 103 panic(fmt.Sprintf(format, args...)) 104 } 105 }