github.com/mgoltzsche/ctnr@v0.7.1-alpha/pkg/errors/typed.go (about) 1 package errors 2 3 import ( 4 "fmt" 5 6 orgerrors "github.com/pkg/errors" 7 ) 8 9 type typed struct { 10 t string 11 cause error 12 } 13 14 func Typed(typeName, msg string) error { 15 return &typed{typeName, orgerrors.New(msg)} 16 } 17 18 func Typedf(typeName, format string, o ...interface{}) error { 19 return &typed{typeName, orgerrors.Errorf(format, o...)} 20 } 21 22 func (e *typed) Type() string { 23 return e.t 24 } 25 26 func (e *typed) Error() string { 27 return e.cause.Error() 28 } 29 30 func (e *typed) Format(s fmt.State, verb rune) { 31 type formatter interface { 32 Format(s fmt.State, verb rune) 33 } 34 e.cause.(formatter).Format(s, verb) 35 } 36 37 func HasType(err error, typeName string) bool { 38 type causer interface { 39 Cause() error 40 } 41 type typed interface { 42 Type() string 43 } 44 if terr, ok := err.(typed); ok && terr.Type() == typeName { 45 return true 46 } 47 if cerr, ok := err.(causer); ok && cerr.Cause() != nil { 48 return HasType(cerr.Cause(), typeName) 49 } 50 return false 51 }