gopkg.in/hedzr/errors.v3@v3.3.1/tool.go (about) 1 package errors 2 3 import ( 4 "runtime" 5 ) 6 7 // DumpStacksAsString returns stack tracing information like 8 // debug.PrintStack() 9 func DumpStacksAsString(allRoutines bool) string { 10 buf := make([]byte, 16384) 11 buf = buf[:runtime.Stack(buf, allRoutines)] 12 // fmt.Printf("=== BEGIN goroutine stack dump ===\n%s\n=== END goroutine stack dump ===\n", buf) 13 return string(buf) 14 } 15 16 // CanAttach tests if err is attach-able 17 func CanAttach(err interface{}) (ok bool) { //nolint:revive 18 _, ok = err.(interface{ Attach(errs ...error) }) 19 if !ok { 20 _, ok = err.(interface { 21 Attach(errs ...error) *WithStackInfo 22 }) 23 } 24 return 25 } 26 27 // CanCause tests if err is cause-able 28 func CanCause(err interface{}) (ok bool) { //nolint:revive 29 _, ok = err.(causer) 30 return 31 } 32 33 // CanCauses tests if err is cause-able 34 func CanCauses(err interface{}) (ok bool) { //nolint:revive 35 _, ok = err.(causers) 36 return 37 } 38 39 // Causes simply returns the wrapped inner errors. 40 // It doesn't consider an wrapped Code entity is an inner error too. 41 // So if you wanna to extract any inner error objects, use 42 // errors.Unwrap for instead. The errors.Unwrap could extract all 43 // of them one by one: 44 // 45 // var err = errors.New("hello").WithErrors(io.EOF, io.ShortBuffers) 46 // var e error = err 47 // for e != nil { 48 // e = errors.Unwrap(err) 49 // } 50 func Causes(err error) (errs []error) { 51 if e, ok := err.(causers); ok { 52 errs = e.Causes() 53 } 54 return 55 } 56 57 // // CanWalk tests if err is walkable 58 // func CanWalk(err error) (ok bool) { 59 // _, ok = err.(Walkable) 60 // return 61 // } 62 // 63 // // CanRange tests if err is range-able 64 // func CanRange(err error) (ok bool) { 65 // _, ok = err.(Ranged) 66 // return 67 // } 68 69 // CanUnwrap tests if err is unwrap-able 70 func CanUnwrap(err interface{}) (ok bool) { //nolint:revive 71 _, ok = err.(interface{ Unwrap() error }) 72 return 73 } 74 75 // CanIs tests if err is is-able 76 func CanIs(err interface{}) (ok bool) { //nolint:revive 77 _, ok = err.(interface{ Is(error) bool }) 78 return 79 } 80 81 // CanAs tests if err is as-able 82 func CanAs(err interface{}) (ok bool) { //nolint:revive 83 _, ok = err.(interface{ As(interface{}) bool }) //nolint:revive 84 return 85 }